问题描述
我正在尝试使代码以我发现的custom_loss运行
但是我遇到了一些形状不兼容的错误。
InvalidArgumentError:不兼容的形状:[0,1]与[19,1] [[{{node loss_65 / dense_165_loss / custom_loss / GreaterEqual}}]]
显然,输入大小必须被batch_size整除。有人知道如何解决这个问题吗?
import tensorflow as tf
import keras.backend as K
from keras import models
from keras.layers import Dense
import numpy as np
def custom_loss(y_true,y_pred):
y_true_next = y_true[1:]
y_pred_next = y_pred[1:]
y_true_tdy = y_true[:-1]
y_pred_tdy = y_pred[:-1]
y_true_diff = tf.subtract(y_true_next,y_true_tdy)
y_pred_diff = tf.subtract(y_pred_next,y_pred_tdy)
standard = tf.zeros_like(y_pred_diff)
y_true_move = tf.greater_equal(y_true_diff,standard)
y_pred_move = tf.greater_equal(y_pred_diff,standard)
y_true_move = tf.reshape(y_true_move,[-1])
y_pred_move = tf.reshape(y_pred_move,[-1])
condition = tf.not_equal(y_true_move,y_pred_move)
indices = tf.where(condition)
ones = tf.ones_like(indices)
indices = tf.add(indices,ones)
indices = K.cast(indices,dtype='int32')
direction_loss = tf.Variable(tf.ones_like(y_pred),dtype='float32')
updates = K.cast(tf.ones_like(indices),dtype='float32')
alpha = 1000
direction_loss = tf.scatter_nd_update(direction_loss,indices,alpha*updates)
custom_loss = K.mean(tf.multiply(K.square(y_true - y_pred),direction_loss),axis=-1)
return custom_loss
def mpl(train_x,train_y,batch_size):
model = models.Sequential()
model.add(Dense(40,batch_input_shape=(batch_size,train_x.shape[1]),activation = "relu"))
model.add(Dense(1))
model.summary()
model.compile(
optimizer = 'adam',loss = custom_loss
)
results = model.fit(
train_x,epochs= 5,batch_size = batch_size,verbose = 1
)
return results
BATCH_SIZE = 20
# works with BATCH_SIZE = 10|20.. dont work with 15|30...
# train_x = np.random.random_sample((500,10))
# train_y = np.random.random_sample((500,1))
# works only with BATCH_SIZE = 1
train_x = np.random.random_sample((841,10))
train_y = np.random.random_sample((841,1))
result = mpl(train_x,BATCH_SIZE)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)