如何保存然后加载此类张量流深度学习模型?

时间:2019-08-07 10:21:05

标签: python-3.x tensorflow machine-learning deep-learning

以下代码是用于构建聊天机器人的模型。我要保存然后使用。尽管我目前可以保存,但是在预测方面,y_pred_cls具有与训练后相同的值。代码如下-

X = tf.placeholder(tf.float32, shape=[None, training.shape[1]], name="inp")
y = tf.placeholder(tf.float32, shape=[None, output.shape[1]], name="output")
actual_cls = tf.argmax(y, axis=1)
print(actual_cls)

def fully_connected_layer(inpt, input_shape, output_shape, name):

    with tf.variable_scope(name) as scope:

        weights = tf.Variable(tf.truncated_normal(shape=[input_shape, output_shape], stddev = 0.05))

        biases = tf.Variable(tf.constant(0.05, shape=[output_shape]))

        layer = tf.matmul(inpt, weights) + biases
        return layer, weights, biases

def activation(inpt, name):

    with tf.variable_scope(name) as scope:
        layer = tf.nn.relu(inpt)

        return layer

fc1, weights, biases = fully_connected_layer(inpt=training, input_shape=training.shape[1], output_shape=8, name='fc1')
activation1 = activation(inpt=fc1, name='activation1') 

fc2, weights, biases = fully_connected_layer(inpt=activation1, input_shape=8, output_shape=8, name='fc2')
activation2 = activation(inpt=fc2, name='activation2')

fc3, weights, biases = fully_connected_layer(inpt=activation2, input_shape=8, output_shape=6, name='fc3')
y_cls = tf.nn.softmax(fc3)    

with tf.name_scope("y_pred_cls"): 
    y_pred_cls = tf.argmax(y_cls, axis=1)

with tf.name_scope("cost"):
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_cls, labels=y)
    cost = tf.reduce_mean(cross_entropy)

with tf.name_scope("optimizer"):
    optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

with tf.name_scope("accuracy"):
    correct_prediction = tf.equal(y_pred_cls, actual_cls)
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

0 个答案:

没有答案