为了账号安全,请及时绑定邮箱和手机立即绑定

我收到此错误“模块“tensorflow._api.v1.keras.layers”

我收到此错误“模块“tensorflow._api.v1.keras.layers”

温温酱 2021-12-09 10:55:21
执行以下代码时出现上述错误。我正在尝试在下面的 tensorflow 神经网络实现教程中解决这个问题。 https://www.datacamp.com/community/tutorials/tensorflow-tutorialdef load_data(data_directory):directories = [d for d in os.listdir(data_directory)                if os.path.isdir(os.path.join(data_directory, d))]labels = []images = []for d in directories:    label_directory = os.path.join(data_directory, d)    file_names = [os.path.join(label_directory, f)                   for f in os.listdir(label_directory)                   if f.endswith(".ppm")]    for f in file_names:        images.append(skimage.data.imread(f))        labels.append(int(d))return images, labelsimport osimport skimagefrom skimage import transformfrom skimage.color import rgb2grayimport numpy as npimport kerasfrom keras import layersfrom keras.layers import DenseROOT_PATH = "C://Users//Jay//AppData//Local//Programs//Python//Python37//Scriptcodes//BelgianSignals"train_data_directory = os.path.join(ROOT_PATH, "Training")test_data_directory = os.path.join(ROOT_PATH, "Testing")images, labels = load_data(train_data_directory)# Print the `labels` dimensionsprint(np.array(labels))# Print the number of `labels`'s elementsprint(np.array(labels).size)# Count the number of labelsprint(len(set(np.array(labels))))# Print the `images` dimensionsprint(np.array(images))# Print the number of `images`'s elementsprint(np.array(images).size)# Print the first instance of `images`np.array(images)[0]images28 = [transform.resize(image, (28, 28)) for image in images]images28 = np.array(images28)images28 = rgb2gray(images28)# Import `tensorflow` import tensorflow as tf # Initialize placeholders x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])y = tf.placeholder(dtype = tf.int32, shape = [None])# Flatten the input dataimages_flat = tf.keras.layers.flatten(x)# Fully connected layer logits = tf.contrib.layers.dense(images_flat, 62, tf.nn.relu)
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

任何一个

from keras.layers import Flatten

并使用

Flatten()(input)

或者

简单地使用

tf.keras.layers.Flatten()(input)


查看完整回答
反对 回复 2021-12-09
?
跃然一笑

TA贡献1826条经验 获得超6个赞

新的(“keras 作为默认 API”)方法会让你使用 keras 层,tf.keras.layers.Flatten但你似乎错过了一些细微差别(评论中没有提到)。


tf.keras.layers.Flatten() 实际上返回一个 keras 层(可调用)对象,该对象又需要与您的前一层一起调用。


所以更像是这样的:


# Flatten the input data

flatten_layer = tf.keras.layers.Flatten()

images_flat = flatten_layer(x)

或者,为简洁起见,只是:


# Flatten the input data

images_flat = tf.keras.layers.Flatten()(x)


查看完整回答
反对 回复 2021-12-09
  • 2 回答
  • 0 关注
  • 311 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信