在执行以下代码时,出现上述错误。 我正在尝试在有关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' UserWarning:在skimage 0.15中,默认模式“恒定”将更改为“反射”。
警告(来自警告模块): 文件“ C:\ Users \ Jay \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ site-packages \ skimage \ transform_warps.py”,第110行 warn(“默认情况下,在skimage 0.15中将启用抗锯齿为” UserWarning:默认情况下,在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'
我正在使用, Keras版本2.2.4 Tensorflow版本1.13.1
答案 0 :(得分:1)
任何一个
from keras.layers import Flatten
并使用
Flatten()(input)
或
只需使用
tf.keras.layers.Flatten()(input)
答案 1 :(得分:0)
新的方法(“作为默认API的keras”)将使您使用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)