这两个代码有什么区别吗?

时间:2017-06-30 04:06:14

标签: machine-learning tensorflow

我目前对Tensorflow还是比较新的。我对这两段代码有些麻烦。

代码A:

self.h1_layer = tf.layers.dense(self.x, self.n_nodes_hl1, activation=tf.nn.relu, name="h1")
self.h2_layer = tf.layers.dense(self.h1_layer, self.n_nodes_hl2, activation=tf.nn.relu, name="h2")
self.h3_layer = tf.layers.dense(self.h2_layer, self.n_nodes_hl3, activation=tf.nn.relu, name="h3")

self.logits = tf.layers.dense(self.h3_layer, self.num_of_classes, name="output")

代码B:

self.hidden_1_layer = {
    'weights': tf.Variable(tf.random_normal([self.num_of_words, self.h1])),
    'biases' : tf.Variable(tf.random_normal([self.h1]))
}

self.hidden_2_layer = {
    'weights': tf.Variable(tf.random_normal([self.h1, self.h2])),
    'biases' : tf.Variable(tf.random_normal([self.h2]))
}

self.hidden_3_layer = {
    'weights': tf.Variable(tf.random_normal([self.h2, self.h3])),
    'biases' : tf.Variable(tf.random_normal([self.h3]))
}

self.final_output_layer = {
    'weights': tf.Variable(tf.random_normal([self.h3, self.num_of_classes])),
    'biases' : tf.Variable(tf.random_normal([self.num_of_classes]))
}

layer1 = tf.add(tf.matmul(data, self.hidden_1_layer['weights']), self.hidden_1_layer['biases'])
layer1 = tf.nn.relu(layer1)

layer2 = tf.add(tf.matmul(layer1, self.hidden_2_layer['weights']), self.hidden_2_layer['biases'])
layer2 = tf.nn.relu(layer2)

layer3 = tf.add(tf.matmul(layer2, self.hidden_3_layer['weights']), self.hidden_3_layer['biases'])
layer3 = tf.nn.relu(layer3)

output = tf.matmul(layer3, self.final_output_layer['weights']) + self.final_output_layer['biases']

它们是一样的吗?代码A和A都可以B权重和偏差用tf.train.Saver()?

保存

由于

编辑: 我正面临使用代码A生成预测的问题。似乎代码A的logits总是在变化。

完整代码:

import tensorflow as tf
import os

from utils import Utils as utils

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

class Neural_Network:
    # Neural Network Setup
    num_of_epoch = 50

    n_nodes_hl1 = 500
    n_nodes_hl2 = 500
    n_nodes_hl3 = 500

    def __init__(self):
        self.num_of_classes = utils.get_num_of_classes()
        self.num_of_words = utils.get_num_of_words()

        # placeholders
        self.x = tf.placeholder(tf.float32, [None, self.num_of_words])
        self.y = tf.placeholder(tf.int32, [None, self.num_of_classes])

        with tf.name_scope("model"):
            self.h1_layer = tf.layers.dense(self.x, self.n_nodes_hl1, activation=tf.nn.relu, name="h1")
            self.h2_layer = tf.layers.dense(self.h1_layer, self.n_nodes_hl2, activation=tf.nn.relu, name="h2")
            self.h3_layer = tf.layers.dense(self.h2_layer, self.n_nodes_hl3, activation=tf.nn.relu, name="h3")

            self.logits = tf.layers.dense(self.h3_layer, self.num_of_classes, name="output")

    def predict(self):
        return self.logits

    def make_prediction(self, query):
        result = None

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

            saver = tf.train.import_meta_graph('saved_models/testing.meta')
            saver.restore(sess, 'saved_models/testing')

            # for variable in tf.trainable_variables():
            #     print sess.run(variable)

            prediction = self.predict()
            pre, prediction = sess.run([self.logits, prediction], feed_dict={self.x : query})
            print pre
            prediction = prediction.tolist()
            prediction = tf.nn.softmax(prediction)
            prediction = sess.run(prediction)
            print prediction

            return utils.get_label_from_encoding(prediction[0])

    def train(self, data):
        prediction = self.predict()

        cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=self.y))
        optimizer = tf.train.AdamOptimizer().minimize(cost)

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

            writer = tf.summary.FileWriter("mygraph/logs", tf.get_default_graph())

            for epoch in range(self.num_of_epoch):
                optimised, loss = sess.run([optimizer, cost],
                                           feed_dict={self.x: data['values'], self.y: data['labels']})

                if epoch % 1 == 0:
                    print("Completed Training Cycle: " + str(epoch) + " out of " + str(self.num_of_epoch))
                    print("Current Loss: " + str(loss))

                    saver = tf.train.Saver()
                    saver.save(sess, 'saved_models/testing')
                    print("Model saved")

1 个答案:

答案 0 :(得分:1)

TLDR :操作基本相同,但变量创建初始化方法不同。

如果从here跟踪代码,最终将进入代码调用tf.get_variable初始化变量的阶段。在上面的示例中,由于未设置kernel_initializerbias_initializer,因此它们将分别默认为Nonetf.zeros_initializer()see Dense API)。将None作为初始化程序传递给tf.get_variable时,将使用glorot_uniform_initializer

  

如果初始化程序为None(默认值),则传递默认初始值设定项   将使用变量范围。如果那个也是无,a   将使用glorot_uniform_initializer。初始化程序也可以是   张量,在这种情况下,变量初始化为此值和   形状

有关tf.get_variable的更多信息,请访问here

对于一种情况,您使用tf.random_normal初始值设定项来获取内核权重和偏差权重,但对于另一种情况,您使用了tf.layers.dense并且将导致glorot_uniform_initializer内核权重和偏差权重为zeros_initializer,因为没有参数传递给tf.layers.dense

关于他们是否可以得救的第二个问题,是的,他们可以。

作为最后一点,使用tf.Variable时必须小心,因为如果未正确设置范围,可能会使事情变得复杂。