Tensorflow:相同的输入数据,不同的输出

时间:2018-11-03 19:07:52

标签: python tensorflow machine-learning neural-network

训练模型后,我将其保存并加载以进行一些测试。但是,每次我重新加载模型时,我得到的精度和结果都是完全相同的输入数据。训练模型后,我会打印精度,并且总是得到一个不错的值(0.8〜0.9),但是当我重新加载时,精度下降到(0.1〜0.5)-我不知道这是否与问题有关btw thats很奇怪。

import tensorflow as tf
import numpy as np
import json

n_nodes_hl1 = 1600
n_nodes_hl2 = 800
n_nodes_hl3 = 400
n_nodes_hl4 = 200

n_classes = 4
batch_size = 50

input_lenght = 65
x = tf.placeholder('float', [None, input_lenght])
y = tf.placeholder('float')


def train_network(x):
    prediction = neural_network_model(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.00001).minimize(cost)

    hm_epochs = 20000
    saver = tf.train.Saver()
    init_op = tf.global_variables_initializer()
    sess = tf.Session()
    sess.run(init_op)
    epoch = 0 

    for epoch in range(hm_epochs):
        epoch_cost = 0
        i = 0
        while i < len(train_x):
            start = i
            end = i + batch_size

            batch_x = np.array(train_x[start:end])
            batch_y = np.array(train_y[start:end])

            _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})

            epoch_cost += c
            i += batch_size

    save_path = saver.save(sess, "drive/My Drive/datasets/tensorflow/model")

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
    print("accuracy:", accuracy.eval({x: test_x, y: test_y}, session=sess))


    sess.close()    

def group_test_train(features_data, labels_data, test_size):
    featureset = []

    for i in range(test_size):
        featureset += [[features_data[i], labels_data[i]]]

    featureset = np.array(featureset)

    np.random.shuffle(featureset)

    train_x = list(featureset[:, 0][:test_size // 2])
    train_y = list(featureset[:, 1][:test_size // 2])

    test_x = list(featureset[:, 0][test_size // 2:])
    test_y = list(featureset[:, 1][test_size // 2:])

    return train_x, train_y, test_x, test_y

def neural_network_model(data):
    hidden1 = {'weights': tf.Variable(tf.random_uniform([input_lenght, n_nodes_hl1], -1, 1)),
               'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))
               }

    hidden2 = {'weights': tf.Variable(tf.random_uniform([n_nodes_hl1, n_nodes_hl2], -1, 1)),
               'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))
               }

    hidden3 = {'weights': tf.Variable(tf.random_uniform([n_nodes_hl2, n_nodes_hl3], -1, 1)),
               'biases': tf.Variable(tf.random_normal([n_nodes_hl3]))
               }

    hidden4 = {'weights': tf.Variable(tf.random_uniform([n_nodes_hl3, n_nodes_hl4], -1, 1)),
               'biases': tf.Variable(tf.random_normal([n_nodes_hl4]))
               }

    l_output = {'weights': tf.Variable(tf.random_uniform([n_nodes_hl4, n_classes], -1, 1)),
                'biases': tf.Variable(tf.random_normal([n_classes]))
                }

    l1 = tf.add(tf.matmul(data, hidden1['weights']), hidden1['biases'])
    l1 = tf.nn.relu(l1)

    l2 = tf.add(tf.matmul(l1, hidden2['weights']), hidden2['biases'])
    l2 = tf.nn.relu(l2)

    l3 = tf.add(tf.matmul(l2, hidden3['weights']), hidden3['biases'])
    l3 = tf.nn.relu(l3)

    l4 = tf.add(tf.matmul(l3, hidden4['weights']), hidden4['biases'])
    l4 = tf.nn.relu(l4)

    output = tf.add(tf.matmul(l4, l_output['weights']), l_output['biases'])

    return output



version = 'end'
with open('drive/My Drive/datasets/json/' + 'data-'+ version +'.json') as json_file:  
    x_, y_ = json.load(json_file)


train_x, train_y, test_x, test_y = group_test_train(x_, y_, len(x_) )
train_network(x)

每次我都在下面运行此零件时,精度也会发生变化,输出也会随之变化。

prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.00001).minimize(cost)

init_op = tf.global_variables_initializer()


sess = tf.Session()
sess.run(init_op)      
new_saver = tf.train.import_meta_graph('drive/My Drive/datasets/tensorflow/model.meta')
new_saver.restore(sess, tf.train.latest_checkpoint('drive/My Drive/datasets/tensorflow/'))


correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

print("accuracy:", accuracy.eval({x: train_x, y: train_y}, session=sess))

0 个答案:

没有答案