计算验证损失与训练损失同时进行

时间:2019-07-23 11:06:46

标签: python tensorflow machine-learning

在权重计算之后(在火车上),我尝试在每次迭代中计算验证损失。如何使用得到的权重张量预测验证集上的值?

我尝试使用两个数组来存储会话每一步的损失值。

X_tr, X_val, y_tr, y_val = train_test_split(train_set, y_train, test_size=0.2, random_state=42)

x = tf.placeholder(tf.float32, X_tr.shape, name = 'data')
y = tf.placeholder(tf.float32, y_tr.shape, name = 'labels')
W = tf.Variable(tf.zeros([len(train_set.columns),1]), dtype = tf.float32, name = 'weights')
b = tf.Variable(0, dtype = tf.float32, name = 'bias')
y_pred = tf.matmul(x, W) + b

loss = tf.reduce_mean(tf.square(y - y_pred), name = 'loss')
optimizer = tf.train.MomentumOptimizer(learning_rate = 0.0006, momentum = 0.90)
train_op = optimizer.minimize(loss)

losses_t, losses_v = [], []
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for i in range(1000):
        _, loss_t = sess.run([train_op, loss], feed_dict = {x: X_tr, y: y_tr})
        losses_t.append(loss_t)
        loss_v = sess.run(loss, feed_dict = {x: X_val, y: y_val})
        losses_v.append(loss_v)
        if(i % 20 == 0):
            print('Training loss is: ', loss_t)
            print('Validation loss is: ', loss_v)

    W_value, b_value = sess.run([W, b])

错误:

ValueError: Cannot feed value of shape (292, 220) for Tensor 'data_6:0', which has shape '(1166, 220)'

1 个答案:

答案 0 :(得分:0)

问题是我给两个sess.run函数都赋予了相同的参数。我改为创建了新的占位符(以适应输入X_val和y_val的形状)和一个损失函数,用于特定于其测试集的验证损失(X_val,y_val)。

losses_t = []
losses_v = []
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for i in range(3000):
        _, loss_t = sess.run([train_op, loss], feed_dict = {x: X_tr, y: y_tr})
        losses_t.append(loss_t)
        loss_v = sess.run(lossv, feed_dict = {xv: X_val, yv: y_val})
        losses_v.append(loss_v)
        if(i % 20 == 0):
            print('Training loss is: ', loss_t)
            print('Validation loss is: ', loss_v)

    W_value, b_value = sess.run([W, b])