我似乎有一个特定的代码,因为我无法在网上找到想要的东西,所以这是我的问题:
我已经编码了一个采用特定长度数组的NN,并且应该给我一个singe值作为输出:
model = tf.keras.Sequential()
model.add(layers.Embedding(input_dim=int(input_len_array), output_dim=8 * int(input_len_array)))
model.add(layers.GRU(32 * int(input_len_array), return_sequences=True))
# Last layer...
model.add(layers.Dense(1, activation='tanh'))
之后,我创建一个Custom_loss函数:
custom_loss(x_, y_):
sess = tf.Session()
Sortino = self.__backtest(x_, y_)
def loss(y_true, y_pred):
print('Sortino: ', Sortino)
# The Optimizer will MAXIMIZE the Sortino so we compute -Sortino
return tf.convert_to_tensor(-Sortino)
return loss
此后,我编译模型,并在张量X和Y中将整批值赋给它:
self.model.compile(optimizer='adam', loss=custom_loss(x, y))
在自定义损失内部,我将函数定义为self .__ backtest,定义如下:
def __backtest(self, x_: tf.Tensor, y_r: tf.Tensor, timesteps=40):
my_list = []
sess = tf.Session()
# Defining the Encoder
# enc = OneHotEncoder(handle_unknown='ignore')
# X = [[-1, 0], [0, 1], [1, 2]]
# enc.fit(X)
# sess.run(x_)[i, :] is <class 'numpy.ndarray'>
print('in backest: int(x_.get_shape())', x_.get_shape())
for i in range(int(x_.get_shape()[0])):
output_of_nn = self.model.predict(sess.run(x_)[i, :] / np.linalg.norm(sess.run(x_)[i, :]))
# categorical_output = tf.keras.utils.to_categorical(output_of_nn)
my_list.append(scaled_output * sess.run(y_r)[i])
if i < 10:
print('First 10 scaled output: ', scaled_output)
if i > 0:
capital_evolution.append(capital_evolution[-1] * (my_list[-1] + 1))
my_array = np.array(my_list)
if len(my_array) < 10:
return -10
try:
Sortino = my_array.mean() / my_array.std()
except:
Sortino = -10
return Sortino
计算机无法运行代码,并给我这个错误:
ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
如果有人可以给出解决方案,我将不胜感激!!非常感谢!