gpt4 book ai didi

python - tensorflow keras : I am getting this error 'module "tensorflow. _api.v1.keras.layers' 没有属性 'flatten'“

转载 作者:行者123 更新时间:2023-11-28 18:58:43 25 4
gpt4 key购买 nike

我在执行下面的代码时遇到了上面的错误。我正在尝试解决以下关于 tensorflow 神经网络实现的教程。 https://www.datacamp.com/community/tutorials/tensorflow-tutorial

def 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, labels

import os
import skimage
from skimage import transform
from skimage.color import rgb2gray
import numpy as np
import keras
from keras import layers
from keras.layers import Dense
ROOT_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` dimensions
print(np.array(labels))

# Print the number of `labels`'s elements
print(np.array(labels).size)

# Count the number of labels
print(len(set(np.array(labels))))

# Print the `images` dimensions
print(np.array(images))

# Print the number of `images`'s elements
print(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 data
images_flat = tf.keras.layers.flatten(x)

# Fully connected layer
logits = tf.contrib.layers.dense(images_flat, 62, tf.nn.relu)

# Define a loss function
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y,
logits = logits))
# Define an optimizer
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# Convert logits to label indexes
correct_pred = tf.argmax(logits, 1)

# Define an accuracy metric
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

起初,我在教程中使用了 tf.layers.flatten(x)。但是,它将在未来版本中贬值。所以按照建议添加 keras。

我在 IDLE 控制台中得到以下输出。

重启:C:\Users\Jay\AppData\Local\Programs\Python\Python37\Scriptcodes\SecondTensorFlow.py使用 TensorFlow 后端。

警告(来自警告模块): 文件“C:\Users\Jay\AppData\Local\Programs\Python\Python37\lib\site-packages\skimage\transform_warps.py”,第 105 行 warn("默认模式,'constant',将更改为'reflect' in "用户警告:默认模式“常量”将在 skimage 0.15 中更改为“反射”。

警告(来自警告模块): 文件“C:\Users\Jay\AppData\Local\Programs\Python\Python37\lib\site-packages\skimage\transform_warps.py”,第 110 行 warn("抗锯齿将默认在 skimage 0.15 到 "用户警告:skimage 0.15 中将默认启用抗锯齿功能,以避免在对图像进行下采样时出现锯齿现象。

追溯(最近的调用最后): 文件“C:\Users\Jay\AppData\Local\Programs\Python\Python37\Scriptcodes\SecondTensorFlow.py”,第 64 行,在

images_flat = tf.python.keras.layers.flatten(x)

AttributeError: 模块 'tensorflow' 没有属性 'python'

我正在使用,凯拉斯版本 2.2.4Tensorflow 版本 1.13.1

最佳答案

要么

from keras.layers import Flatten

和使用

Flatten()(输入)

简单使用

tf.keras.layers.Flatten()(输入)

关于python - tensorflow keras : I am getting this error 'module "tensorflow. _api.v1.keras.layers' 没有属性 'flatten'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com