Tensorflow:AttributeError:'NoneType'对象没有属性'original_name_scope' [英] Tensorflow: AttributeError: 'NoneType' object has no attribute 'original_name_scope'

查看:183
本文介绍了Tensorflow:AttributeError:'NoneType'对象没有属性'original_name_scope'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Cloud的Debian 9.5拉伸系统上运行一些python tensorflow代码.我正在使用此tensorflow GPU版本(最新版本)并安装了合适的CODA和cuDNN软件.这是我的代码:

I am trying to run some python tensorflow code on a debian 9.5 stretch system on google cloud. I am Using tensorflow GPU version of this (the latest version) with the approriate CODA and cuDNN software installed. Here is my code:

     import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()


x_train = tf.keras.utils.normalize(x_train, axis=1) # scales all values between 0 and 1 on pixel image
x_test = tf.keras.utils.normalize(x_test, axis=1)

model = tf.keras.models.Sequential()
model.add (tf.keras.layers.Flatten()) # flattens the 28x28 pixels into long flat vector
model.add(tf.layers.Dense(128, activation=tf.nn.relu))# builds hidden layer 128 neurons (we can tweak) and activation func - use this as default, sigmoid function
model.add(tf.layers.Dense(128, activation=tf.nn.relu)) # second layer
model.add(tf.layers.Dense(10, activation=tf.nn.softmax)) # output layer - number of output neurons =classes
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy'])  # adam is least squares test

model.fit(x_train, y_train, epochs=3)
#model.fit(x_train, y_train, epochs=3)

但是,出现以下错误:

AttributeError跟踪(最近一次呼叫最近) <

AttributeError Traceback (most recent call last) <

ipython-input-1-3604b3cbf07d> in <module>
     16 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics = ['accuracy'])  # adam is least squares test
     17 
---> 18 model.fit(x_train, y_train, epochs=3)
     19 #model.fit(x_train, y_train, epochs=3)
     20 

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1507         steps_name='steps_per_epoch',
   1508         steps=steps_per_epoch,
-> 1509         validation_split=validation_split)
   1510 
   1511     # Prepare validation data.

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, batch_size, check_steps, steps_name, steps, validation_split)
    991       x, y = next_element
    992     x, y, sample_weights = self._standardize_weights(x, y, sample_weight,
--> 993                                                      class_weight, batch_size)
    994     return x, y, sample_weights
    995 

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _standardize_weights(self, x, y, sample_weight, class_weight, batch_size)
   1027       if not self.inputs:
   1028         is_build_called = True
-> 1029         self._set_inputs(x)
   1030 
   1031     if y is not None:

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py in _method_wrapper(self, *args, **kwargs)
    424     self._setattr_tracking = False  # pylint: disable=protected-access
    425     try:
--> 426       method(self, *args, **kwargs)
    427     finally:
    428       self._setattr_tracking = previous_value  # pylint: disable=protected-access

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py in _set_inputs(self, inputs, training)
   1220       else:
   1221         input_shape = (None,) + inputs.shape[1:]
-> 1222         self.build(input_shape=input_shape)
   1223     if context.executing_eagerly():
   1224       self._eager_set_inputs(inputs)

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py in build(self, input_shape)
    219       for layer in self.layers:
    220         if not layer.built:
--> 221           with ops.name_scope(layer._name_scope()):
    222             layer.build(shape)
    223           layer.built = True

~/Python-3.6.4/py_36_env/lib/python3.6/site-packages/tensorflow/python/layers/base.py in _name_scope(self)
    139   def _name_scope(self):
    140     """Determines op naming for the Layer."""
--> 141     return self._current_scope.original_name_scope
    142 
    143   def _set_scope(self, scope=None):

AttributeError: 'NoneType' object has no attribute 'original_name_scope'

此代码在使用CPU版本的tensorflow的笔记本电脑上可以正常工作,但是我在虚拟机上收到此错误.有什么想法吗?

This code works fine on my laptop using the CPU version of tensorflow, however I am getting this error on the virtual machine. Any ideas?

推荐答案

我遇到了类似的问题,解决方案是使用

i had the similar issue and the solution is to use

model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))

代替

model.add(tf.layers.Dense(128, activation=tf.nn.relu))

保存模型需要

使用@Josh的答案.

answer by @Josh will be required for saving the model.

这篇关于Tensorflow:AttributeError:'NoneType'对象没有属性'original_name_scope'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆