python – Tensorflow权重矩阵排名错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Tensorflow权重矩阵排名错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3086字,纯文字阅读大概需要5分钟。
内容图文
import tensorflow as tf
import numpy as np
import os
from PIL import Image
cur_dir = os.getcwd()
def modify_image(image):
resized = tf.image.resize_images(image, 180, 180, 1)
resized.set_shape([180,180,3])
flipped_images = tf.image.flip_up_down(resized)
return flipped_images
def read_image(filename_queue):
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
image = tf.image.decode_jpeg(value)
return key,image
def inputs():
filenames = ['standard_1.jpg', 'standard_2.jpg' ]
filename_queue = tf.train.string_input_producer(filenames)
filename,read_input = read_image(filename_queue)
reshaped_image = modify_image(read_input)
reshaped_image = tf.cast(reshaped_image, tf.float32)
label=tf.constant([1])
return reshaped_image,label
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
image,label = inputs()
W_conv1=weight_variable([5,5,3,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2=weights_variable([5,5,32,64])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([8 * 8 * 32, 512])
b_fc1 = bias_variable([512])
h_pool2_flat = tf.reshape(h_pool2, [-1, 8*8*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = weight_variable([512, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
tf.train.start_queue_runners(sess=sess)
for i in xrange(100):
img,label = sess.run(image)
print (label)
train_step.run({img, label, 0.5})
当我运行代码时,我收到错误,
"ValueError: ShapesTensorShape([Dimension(180),Dimension(180),Dimension(3)]) and TensorShape([Dimension(None), Dimension(None), Dimension(None), Dimension(None)]) must have the same rank"
但权重已经初始化,即便如此,它也显示为空张量.
正在读取和传输文件和标签.
第一个卷积层有一个5×5窗口,深度为3,我希望有32个这样的5X5滤镜.因此W_conv1的形状为[5,5,3,32].
解决方法:
inputs()函数返回180 x 180 x 3的张量,但tf.nn.conv2d()需要4-D张量的形状batch_size x height x width x num_channels.
As etarion suggests,你可以通过重塑图像张量来完成这项工作(例如使用image = tf.expand_dims(image,0)).但是,如果您正在训练神经网络,您可能希望批量输入.一种方法是使用tf.train.batch():
image, label = inputs()
# Set batch_size to the largest value that works for your configuration.
image_batch, label_batch = tf.train.batch([image, label], batch_size=32)
…然后使用image_batch或label_batch分别使用图像和标签.
内容总结
以上是互联网集市为您收集整理的python – Tensorflow权重矩阵排名错误全部内容,希望文章能够帮你解决python – Tensorflow权重矩阵排名错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。