Reputation: 77
I am getting the above error while executing the below code. I am trying to work out this below tutorial on tensorflow neural network implementation. 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))
At first, I used tf.layers.flatten(x) as in the tutorial. however, it will be depreciated in future versions. So add keras instead as suggested.
I am getting the following output in IDLE Console.
RESTART: C:\Users\Jay\AppData\Local\Programs\Python\Python37\Scriptcodes\SecondTensorFlow.py Using TensorFlow backend.
Warning (from warnings module): File "C:\Users\Jay\AppData\Local\Programs\Python\Python37\lib\site-packages\skimage\transform_warps.py", line 105 warn("The default mode, 'constant', will be changed to 'reflect' in " UserWarning: The default mode, 'constant', will be changed to 'reflect' in skimage 0.15.
Warning (from warnings module): File "C:\Users\Jay\AppData\Local\Programs\Python\Python37\lib\site-packages\skimage\transform_warps.py", line 110 warn("Anti-aliasing will be enabled by default in skimage 0.15 to " UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images.
Traceback (most recent call last): File "C:\Users\Jay\AppData\Local\Programs\Python\Python37\Scriptcodes\SecondTensorFlow.py", line 64, in
images_flat = tf.python.keras.layers.flatten(x)
AttributeError: module 'tensorflow' has no attribute 'python'
I am using, Keras version 2.2.4 Tensorflow version 1.13.1
Upvotes: 6
Views: 10673
Reputation: 11
Either
from keras.layers import Flatten
and use
Flatten()(input)
or
simply use
tf.keras.layers.Flatten()(input)
Upvotes: 1
Reputation: 14515
The new ("keras as the default API") approach would have you use the keras layer tf.keras.layers.Flatten
but there is a little nuance you seem to have missed (and that hasn't been mentioned in the comments).
tf.keras.layers.Flatten()
actually returns a keras layer (callable) object which in turn needs to be called with your previous layer.
So something more like this:
# Flatten the input data
flatten_layer = tf.keras.layers.Flatten()
images_flat = flatten_layer(x)
or, for brevity, just:
# Flatten the input data
images_flat = tf.keras.layers.Flatten()(x)
Upvotes: 0