tensorflow恢复变量奇怪的值

时间:2018-06-05 15:22:32

标签: tensorflow

我正在训练一个关于基本分类问题的NN。 NN工作正常,我设法保存它没有任何错误,但当我尝试恢复(我也能做没有任何错误),恢复的变量得到所有奇怪的值,即不是与训练的相同的值价值,加上预测因此而变得非常糟糕。我尝试手动保存变量的最后更新值(权重和偏差),即它们是否经过完全训练(这是我想要保存和恢复的值)但是那些与我设法的那些完全不同恢复。我无法弄清楚我做错了什么,我花了这么多时间谷歌搜索。任何意见都将非常感谢,谢谢!!

x_nn = tf.placeholder("float32", [None, len(x_nn_train_data[0])])
y_nn = tf.placeholder(dtype = tf.int32, shape=[None])
y_nn = tf.one_hot(y_nn, depth = 2)

n_classes = 2
n_nodes_hl1 = 20  
n_nodes_hl2 = 10 
batch_size = 50
num_hm_epochs = 3

hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([len(x_nn_train_data[0]), n_nodes_hl1]), name='hl1_w_var'),
                  'biases': tf.Variable(tf.random_normal([n_nodes_hl1]), name='hl1_b_var')}

hidden_layer_2 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2]), name='hl2_w_var'),
                  'biases': tf.Variable(tf.random_normal([n_nodes_hl2]), name='hl2_b_var')}

output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes]), name='output_w_var'),
                 'biases': tf.Variable(tf.random_normal([n_classes]), name='output_b_var')}

# # # # # # # # #

prediction = create_prediction_output(x_nn, hidden_layer_1, hidden_layer_2, output_layer)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = prediction, labels = y_nn))
optimizer = tf.train.AdamOptimizer().minimize(cost)
init = tf.global_variables_initializer()
saver = tf.train.Saver()

# # # # # # # # #

def create_prediction_output(train_data, hidden_layer_1, hidden_layer_2, output_layer):       
    l1 = tf.nn.relu(tf.add(tf.matmul(train_data, tf.cast(hidden_layer_1['weights'], tf.float32)), tf.cast(hidden_layer_1['biases'], tf.float32)), name='hl1_oper')

    l2 = tf.nn.relu(tf.add(tf.matmul(l1, tf.cast(hidden_layer_2['weights'], tf.float32)), tf.cast(hidden_layer_2['biases'], tf.float32)), name='hl2_oper')

    output = tf.add(tf.matmul(l2, tf.cast(output_layer['weights'], tf.float32)), tf.cast(output_layer['biases'], tf.float32), name='output_oper')

    return(output)

# # # # # # # # # # #

def train_nn(x_nn):
    hm_epochs = num_hm_epochs 

    with tf.Session() as sess:
        sess.run(init)

        for epoch in range(0, hm_epochs):
            epoch_loss = 0

            for i in range(0, int(len(x_nn_train_data)/batch_size)):
                start = i * batch_size
                x_epoch = x_nn_train_data[start: start+batch_size]
                y_epoch = y_nn_train_data[start: start+batch_size]
                i, c = sess.run([optimizer, cost], feed_dict={x_nn: x_epoch, y_nn: y_epoch})
                epoch_loss += c


            print(epoch+1, hm_epochs, epoch_loss)

            correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y_nn,1))
            accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))          

            print('Accuracy:', accuracy.eval({x_nn: x_nn_test_data, y_nn: y_nn_test_data}))

        saver.save(sess, './model_final')
        hidden_layer_1_output = sess.run(hidden_layer_1)
        hidden_layer_2_output = sess.run(hidden_layer_2)
        output_layer_output =  sess.run(output_layer)

        prediction_class = tf.argmax(prediction,1).eval({x_nn: x_nn_test_data})
        prediction_prob = tf.nn.softmax(prediction, 1)
        prediction_prob = prediction_prob.eval({x_nn: x_nn_test_data})
        prediction_val = prediction.eval({x_nn: x_nn_test_data})


        return(prediction_val, prediction_prob, prediction_class, hidden_layer_1_output, hidden_layer_2_output, output_layer_output)

# # # # # # #

def restore_predict_nn(x_nn):
    with tf.Session() as sess:
        saver_imported.restore(sess, tf.train.latest_checkpoint('./'))  

        hl1_w_restore, hl1_b_restore, hl2_w_restore, hl2_b_restore, output_w_restore, output_b_restore = sess.run(["hl1_w_var:0", "hl1_b_var:0", "hl2_w_var:0", "hl2_b_var:0", "output_w_var:0", "output_b_var:0"])

        print(sess.run("output_w_var:0"))

        l1_pred = tf.nn.relu(tf.add(tf.matmul(x_nn, hl1_w_restore), hl1_b_restore))
        l2_pred = tf.nn.relu(tf.add(tf.matmul(l1_pred, hl2_w_restore), hl2_b_restore))
        output_pred = tf.add(tf.matmul(l2_pred, output_w_restore), output_b_restore)

        prediction_prob = tf.nn.softmax(output_pred, 1)

        return(output_pred, prediction_prob)

# # # # # # # # # #

# train the network
prediction_nn_val, prediction_nn_prob, prediction_nn_class, hidden_layer_1_output, hidden_layer_2_output, output_layer_output  = train_nn(x_nn)

# restore the network and make predictions for "new" data
tf.reset_default_graph()
saver_imported = tf.train.import_meta_graph("model_final.meta")
pred_new_class_tensor, pred_new_probs_tensor = restore_predict_nn(x_nn)
with tf.Session(graph = pred_new_probs_tensor.graph) as sess:
    prediction_new_prob = pred_new_probs_tensor.eval({x_nn: x_nn_test_data})
    prediction_new_class = tf.argmax(pred_new_class_tensor,1).eval({x_nn: x_nn_test_data})

0 个答案:

没有答案