将验证损失添加到每个时期的张量流时间序列中

时间:2019-12-24 19:28:54

标签: python tensorflow

我有一些时间序列数据的张量流模型:

test_size = 1000
df = pd.read_csv("data.csv")
df_train = df_log.iloc[:-test_size]
df_test = df_log.iloc[-test_size:]

def calculate_loss(real, predict):
    real = np.array(real)
    predict = np.array(predict)
    pct = 1 - np.sqrt(np.mean(np.square((real - predict) / real)))
    return pct * 100

tf.reset_default_graph()
modelnn = Model(
    learning_rate, num_layers, df_log.shape[1], size_layer, df_log.shape[1], dropout_rate
)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())

for i in range(epoch):
    init_value = np.zeros((1, num_layers * 2 * size_layer))
    total_loss, total_acc = [], []
    for k in range(0, df_train.shape[0] - 1, timestamp):
        index = min(k + timestamp, df_train.shape[0] - 1)
        batch_x = np.expand_dims(
            df_train.iloc[k : index, :].values, axis = 0
        )
        batch_y = df_train.iloc[k + 1 : index + 1, :].values
        logits, last_state, _, loss = sess.run(
            [modelnn.logits, modelnn.last_state, modelnn.optimizer, modelnn.cost],
            feed_dict = {
                modelnn.X: batch_x,
                modelnn.Y: batch_y,
                modelnn.hidden_layer: init_value,
            },
        )        
        init_value = last_state
        total_loss.append(loss)
        total_acc.append(calculate_loss(batch_y[:, 0], logits[:, 0]))
    print("LOSS", np.mean(total_loss), "Accuracy", np.mean(total_acc))

在每个纪元末尾,它都提到了total_acc,我有一个自定义方法。在每个时期结束时如何使用验证数据total_loss来获取模型如何执行的total_accdf_test

0 个答案:

没有答案