【发布时间】:2018-03-21 13:38:38
【问题描述】:
我正在尝试在 keras 中构建自定义损失函数。不幸的是,我对张量流知之甚少。有没有办法可以将传入的张量转换为 numpy 数组,以便计算损失函数?
这是我的功能:
def getBalance(x_true, x_pred):
x_true = np.round(x_true)
x_pred = np.round(x_pred)
NumberOfBars = len(x_true)
NumberOfHours = NumberOfBars/60
TradeIndex = np.where( x_pred[:,1] == 0 )[0]
##remove predictions that are not tradable
x_true = np.delete(x_true[:,0], TradeIndex)
x_pred = np.delete(x_pred[:,0], TradeIndex)
CM = confusion_matrix(x_true, x_pred)
correctPredictions = CM[0,0]+CM[1,1]
wrongPredictions = CM[1,0]+CM[0,1]
TotalTrades = correctPredictions+wrongPredictions
Accuracy = (correctPredictions/TotalTrades)*100
return Accuracy
如果无法使用 numpy 数组,使用 tensorflow 计算该函数的最佳方法是什么?任何方向将不胜感激,谢谢!
编辑 1: 这是我的模型的一些细节。我正在使用一个丢失严重的 LSTM 网络。输入是多变量多时间步长。 输出是二进制数字的二维数组 (20000,2)
model = Sequential()
model.add(Dropout(0.4, input_shape=(train_input_data_NN.shape[1], train_input_data_NN.shape[2])))
model.add(LSTM(30, dropout=0.4, recurrent_dropout=0.4))
model.add(Dense(2))
model.compile(loss='getBalance', optimizer='adam')
history = model.fit(train_input_data_NN, outputs_NN, epochs=50, batch_size=64, verbose=1, validation_data=(test_input_data_NN, outputs_NN_test))
【问题讨论】:
标签: python tensorflow keras