使损失最小化时,变量['Variable:0','Variable:0']不存在渐变。(tensorflow)

时间:2019-12-03 15:14:39

标签: tensorflow

我用张量流实现了神经网络模型。

但是代码无法正常工作。

环境:

OS:Linux

CPU:x86_64(Intel CPU)

框架:张量流(版本2,Python3)

以下是代码

import tensorflow as tf

tf.random.set_seed(seed=1234)

class DenseLayer(object):
       def __init__(self, input_dimention, output_dimention):
              self.list = []
              self.w = tf.Variable(tf.random.normal([input_dimention, output_dimention], dtype=tf.float64))
              self.b = tf.Variable(tf.random.normal([1,], dtype=tf.float64))
              self.list.append(self.w)
              self.list.append(self.b)

       def dense_layer(self, inputs, no_activation):
              mean, var = tf.nn.moments(inputs, [0, 1])
              inputs      = tf.nn.batch_normalization(inputs, mean, var, 0, 1, 0.00001)
              z              = tf.matmul(inputs, self.w) + self.b
              if no_activation==False:
                     return tf.nn.relu(inputs)
              return z

class Model(object):
        def __init__(self):
            self.var_list = []
            self.alpha   = 0.01
            self.lamb    = 0.1
            self.epochs = 1

        #モデルの定義と初期化を行う
        def define_model(self):

            self.dense_layer1 = DenseLayer(28*28, 100)
            self.push_parameter(self.dense_layer1)

            self.dense_layer2 = DenseLayer(28*28, 10)
            self.push_parameter(self.dense_layer2)

        def push_parameter(self, layer):
            for param in layer.list:
                self.var_list.append(param)
            return

        def run(self, inputs):
            inputs           = self.flattend(inputs, 28,28,1)
            dense_layer1 = self.dense_layer1.dense_layer(inputs, False)
            dense_layer2 = self.dense_layer2.dense_layer(dense_layer1, True)

            return dense_layer2

        def loss(self, outputs, targets):
            predicted_y = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = outputs, labels = targets))
            return predicted_y

        def grad(self, x, target_y):
            with tf.GradientTape() as tape:
                tape.watch(self.var_list)
                loss_value = self.loss(self.run(x), target_y)
            return tape.gradient(loss_value, self.var_list)

        def flattend(self, inputs, x, y, z):
            inputs = tf.cast(inputs, tf.float64)
            return tf.reshape(inputs, [-1, x*y*z])

        def optimizer(self):
            opt = tf.keras.optimizers.SGD(learning_rate=self.alpha)
            return opt

        def model_train(self):
            mnist = tf.keras.datasets.mnist
            (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

            train_images = train_images/255.0
            test_images  = test_images/255.0
            train_labels = make_onehot_labels(train_labels)
            test_labels  = make_onehot_labels(test_labels)

            ds_train_x = tf.data.Dataset.from_tensor_slices(train_images)
            ds_train_y = tf.data.Dataset.from_tensor_slices(train_labels)
            train_dataset = tf.data.Dataset.zip((ds_train_x, ds_train_y)).shuffle(buffer_size=10).repeat().batch(100)


            train_images = tf.convert_to_tensor(train_images)
            train_labels   = tf.convert_to_tensor(train_labels)


            test_images = tf.convert_to_tensor(test_images)
            test_labels   = tf.convert_to_tensor(test_labels)

            self.define_model()
            opt = self.optimizer()
            print(self.var_list)
            for epoch in range(self.epochs):
                for (batch, (images, labels)) in enumerate(train_dataset):

                    if batch ==600:
                        break

                    grads = self.grad(images, labels)  

                    opt.apply_gradients(zip(grads, self.var_list))
def make_onehot_labels(labels):
    depth = 10
    one_hot_labels = tf.one_hot(labels, depth)
    return one_hot_labels

以下是执行代码时的错误。

此问题与渐变有关。

请告诉我为什么发生此问题。

 Gradients do not exist for variables ['Variable:0', 'Variable:0'] when minimizing the loss.

0 个答案:

没有答案