我正在尝试遵循this Keras教程,但是在使用命令import re
pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
with open('test.txt', 'r') as rf:
content = rf.read()
matches = pattern.findall(content)
open('iters.txt', 'w').close()
for match in matches:
with open('iters.txt', 'a') as wf:
wf.write(match + '\n')
进行编译时遇到以下错误:
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
Python版本:
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')
操作系统:
MacOS High Sierra
我也在命令行Python 3.6.6
环境中完成所有操作。
如果人们可以抽出时间来帮助我,我将不胜感激。
答案 0 :(得分:2)
首先,python向您发出信号,表示脚本范围内不存在名称为layers
的对象。
但是实际的错误是代码是从TensorFlow's Keras documentation中复制出来的,但是在文档中,代码的第二部分仅用于说明在model.add(...)
调用中实例化的内容。 / p>
因此,请仅删除所有以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 Documentation上了解 Keras 。