变量不存在渐变

时间:2019-12-04 05:36:07

标签: tensorflow

我在Python3上使用tensorflow(framework)实现了基本的神经网络模型

但是代码无法正常工作。

我不明白为什么代码可以正常工作。

执行代码时,会发生错误。

请告诉我如何解决此问题。

以下是代码。

import tensorflow as tf
import numpy as np

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 __call__(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 define_model(self):

            self.dense_layer1 = DenseLayer(28*28, 100)
            self.var_list.append(self.dense_layer1.w)
            self.var_list.append(self.dense_layer1.b)


            self.dense_layer2 = DenseLayer(28*28, 10)
            self.var_list.append(self.dense_layer2.w)
            self.var_list.append(self.dense_layer2.b)

        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()
            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

0 个答案:

没有答案