对象不支持张量流中的项目分配 [英] object does not support item assignment in tensor flow

查看:36
本文介绍了对象不支持张量流中的项目分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在之前的简单程序中,我无法完成一个简单的任务并得到以下错误.

In the simple program before I am not able to do a simple task and get the following error.

import tensorflow as tf

x_1= tf.constant([1, 2, 3])
x_1= tf.reshape(x_1, shape= (1, 3))
x_2= tf.constant([2, 3, 4])
x_2= tf.reshape(x_2, shape= (1, 3))
x_3= tf.constant([3, 4, 5])
x_3= tf.reshape(x_3, shape= (1, 3))
x= tf.concat((x_1, x_2, x_3), axis=0)

for i in range(0, 3):
    x[i, :]= x[i, :]+ 1

init= tf.global_variables_initializer()

with tf.Session() as sess:
   y= sess.run(x)

我收到以下错误:

TypeError: 'Tensor' 对象不支持项目分配

TypeError: 'Tensor' object does not support item assignment

推荐答案

张量对象不能被索引访问/修改.

Tensor objects cannot be accessed/modified by index.

这是修复的代码:

import tensorflow as tf

x_1 = tf.constant([1, 2, 3])
x_1 = tf.reshape(x_1, shape=(1, 3))
x_2 = tf.constant([2, 3, 4])
x_2 = tf.reshape(x_2, shape=(1, 3))
x_3 = tf.constant([3, 4, 5])
x_3 = tf.reshape(x_3, shape=(1, 3))
x = tf.concat((x_1, x_2, x_3), axis=0)

x = tf.add(x, tf.constant(1, shape=x.shape))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    y = sess.run(x)
    print(y)

这篇关于对象不支持张量流中的项目分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆