小编Aar*_*aac的帖子

将张量转换为Numpy数组 - 自定义丢失功能在keras中

我正在尝试在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 
Run Code Online (Sandbox Code Playgroud)

如果不能使用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))
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

5
推荐指数
1
解决办法
4303
查看次数

标签 统计

keras ×1

python ×1

tensorflow ×1