使用tensorflow1.15版本,在生成占位符placeholder时,发生报错module 'tensorflow' has no attribute 'placeholder'

使用tensorflow1.15版本,在生成占位符placeholder时,发生报错module ‘tensorflow’ has no attribute ‘placeholder’

请提供您编写的代码,以及可以使用tf.__version__确认您使用的TensorFlow版本是不是1.15

from tensorflow_core.examples.tutorials.mnist import input_data
import tensorflow_core as tf

from tensorflow_core import placeholder

import numpy as np

import matplotlib.pyplot as plt

author = ‘yasaka’

my_mnist.SOURCE_URL = “MNIST handwritten digit database, Yann LeCun, Corinna Cortes and Chris Burges

my_mnist = input_data.read_data_sets(“MNIST_data_bak/”, one_hot=True)

The MNIST data is split into three parts:

55,000 data points of training data (mnist.train)

10,000 points of test data (mnist.test), and

5,000 points of validation data (mnist.validation).

Each image is 28 pixels by 28 pixels

输入的是一堆图片,None表示不限输入条数,784表示每张图片都是一个784个像素值的一维向量

所以输入的矩阵是None乘以784二维矩阵

x = tf.placeholder(dtype=tf.float32, shape=(None, 784))

初始化都是0,二维矩阵784乘以10个W值

W = tf.Variable(tf.random_uniform([784, 10]))
b = tf.Variable(tf.zeros([10]))

y_pred = tf.nn.softmax(tf.matmul(x, W) + b)

训练

labels是每张图片都对应一个one-hot的10个值的向量

y = tf.placeholder(dtype=tf.float32, shape=(None, 10))

定义损失函数,交叉熵损失函数

对于多分类问题,通常使用交叉熵损失函数

reduction_indices等价于axis,指明按照每行加,还是按照每列加

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y * tf.log(y_pred), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

评估

tf.argmax()是一个从tensor中寻找最大值的序号,tf.argmax就是求各个预测的数字中概率最大的那一个

correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))

用tf.cast将之前correct_prediction输出的bool值转换为float32,再求平均

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

初始化变量

with tf.Session() as sess:
tf.global_variables_initializer().run()
for _ in range(1000):
batch_xs, batch_ys = my_mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
print("TrainSet batch acc : %s " % accuracy.eval({x: batch_xs, y: batch_ys}))
print(“ValidSet acc : %s” % accuracy.eval({x: my_mnist.validation.images, y: my_mnist.validation.labels}))

# 测试
print("TestSet acc : %s" % accuracy.eval({x: my_mnist.test.images, y: my_mnist.test.labels}))

版本 tensorflow1.15,该问题我将 import tensorflow as tf ,同时使用 from tensorflow_core import placeholder时问题得以解决。