Keras教程错误:NameError:未定义名称"layers" [英] Keras Tutorial Error: NameError: name 'layers' is not defined

查看:585
本文介绍了Keras教程错误:NameError:未定义名称"layers"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循 Keras教程,但是在编译时遇到以下错误使用命令 python3 test.py :

I am trying to follow this Keras tutorial, but I encounter the following error when compiling using the command 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

Python version: 3.6.6

操作系统:MacOS High Sierra

Operating System: MacOS High Sierra

我也在命令行(tensorflow)$ 环境中完成所有操作.

I am also doing this all in the command line (tensorflow)$ environment.

推荐答案

出了什么问题

首先,python向您发出信号,表示脚本范围内不存在名称为 layers 的对象.

但是实际的错误是代码是从 TensorFlow的Keras文档中复制的,在文档中,代码的第二部分仅用于说明在 model.add(...)调用中实例化的内容.

But the actual error is that the code was copied out of the TensorFlow's Keras documentation, but in the documentation the second part of the code serves only to explain what is being instantiated within the model.add(...) call.

因此,请仅删除所有以 layers 开头的代码,这只是一个解释.

So just drop all the code that begins with layers, as it is just an explanation.

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文档中了解有关 Keras 的信息..

这篇关于Keras教程错误:NameError:未定义名称"layers"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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