python找不到reshape_python – AttributeError:’Tensor’对象没有属性’reshape’

在尝试可视化自动编码器的重建图像时,遇到错误:AttributeError:'Tensor'对象没有属性'reshape'。原因是尝试在 TensorFlow 的 Tensor 对象上直接调用 numpy 的 reshape 方法。解决方案是使用 TensorFlow 的 tf.reshape 函数,或者在生成噪声后将 Tensor 转换为 numpy 数组再进行 reshape 操作。
摘要由CSDN通过智能技术生成

我想写一个去噪自动编码器,为了可视化的目的,我想打印出损坏的图像.

这是我想要显示损坏图像的测试部分:

def corrupt(x):

noise = tf.random_normal(shape=tf.shape(x), mean=0.0, stddev=0.2, dtype=tf.float32)

return x + noise

# Testing

# Encode and decode images from test set and visualize their reconstruction

n = 10

canvas_orig = np.empty((28, 28 * n))

canvas_corrupt = np.empty((28, 28 * n))

canvas_recon = np.empty((28, 28 * n))

# MNIST test set

batch_x, _ = mnist.test.next_batch(n)

# Encode and decode the digit image and determine the test loss

g, l = sess.run([Y, loss], feed_dict={X: batch_x})

# Draw the generated digits

for i in range(n):

# Original images

canvas_orig[0: 28, i * 28: (i + 1) * 28] = batch_x[i].reshape([28, 28])

# Corrupted images

canvas_corrupt[0: 28, i * 28: (i + 1) * 28] = corrupt(batch_x[i]).reshape([28, 28])

# Reconstructed images

canvas_recon[0: 28, i * 28: (i + 1) * 28] = g[i].reshape([28, 28])

print("Original Images")

plt.figure(figsize=(n, 1))

plt.imshow(canvas_orig, origin="upper", cmap="gray")

plt.show()

print("Corrupted Images")

plt.figure(figsize=(n, 1))

plt.imshow(canvas_corrupt, origin="upper", cmap="gray")

plt.show()

print("Reconstructed Images")

plt.figure(figsize=(n, 1))

plt.imshow(canvas_recon, origin="upper", cmap="gray")

plt.show()

错误发生在以下行:

canvas_corrupt[0: 28, i * 28: (i + 1) * 28] = corrupt(batch_x[i]).reshape([28, 28])

我真的不明白为什么它不起作用,因为它上面和下面的陈述看起来非常相似并且工作得很好.

事实上,“重塑”是一种功能,而不是一种属性,让我感到很困惑.

解决方法:

区别在于batch_x [i]是一个numpy数组(有一个reshape方法),而corrupt(…)的结果是一个Tensor对象.从tf 1.5开始,它没有重塑方法.这不会抛出错误:tf.reshape(corrupt(batch_x [i]),[28,28]))

但是,由于你的目标是可视化值,你应该更好地避免混淆tensorflow和numpy操作并仅在numpy方面重写腐败:

def corrupt(x):

noise = np.random.normal(size=x.shape, loc=0.0, scale=0.2)

return x + noise

标签:python,neural-network,numpy,tensorflow,autoencoder

来源: https://codeday.me/bug/20190705/1389465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值