我正在尝试跟随this https://www.tensorflow.org/guide/kerasKeras教程,但是使用命令编译时遇到以下错误python3 test.py
:
Traceback (most recent call last):
File "test.py", line 13, in <module>
layers.Dense(64, activation='sigmoid')
NameError: name 'layers' is not defined
我的代码如下:
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(keras.layers.Dense(64, activation='relu'))
# Add another:
model.add(keras.layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(keras.layers.Dense(10, activation='softmax'))
# Create a sigmoid layer:
layers.Dense(64, activation='sigmoid')
# A linear layer with L1 regularization of factor 0.01 applied to the kernel matrix:
layers.Dense(64, kernel_regularizer=keras.regularizers.l1(0.01))
# A linear layer with L2 regularization of factor 0.01 applied to the bias vector:
layers.Dense(64, bias_regularizer=keras.regularizers.l2(0.01))
# A linear layer with a kernel initialized to a random orthogonal matrix:
layers.Dense(64, kernel_initializer='orthogonal')
Python版本:3.6.6
操作系统:MacOS High Sierra
我也在命令行中完成这一切(tensorflow)$
环境。
怎么了
首先,python 向您发出信号,一个具有名称的对象layers
不存在于脚本范围内。
但实际的错误是代码是从TensorFlow 的 Keras 文档 https://www.tensorflow.org/guide/keras,但在文档中,代码的第二部分仅用于解释在model.add(...)
call.
所以只需删除所有以layers
,因为这只是一个解释。
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(keras.layers.Dense(64, activation='relu'))
# Add another:
model.add(keras.layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(keras.layers.Dense(10, activation='softmax'))
进一步阅读
你应该考虑学习Keras on the Keras 文档 https://keras.io/getting-started/sequential-model-guide/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)