Python 构建第一个轴形状为“的条件图”;无”;在张量流中
在图形构建阶段,假设张量Python 构建第一个轴形状为“的条件图”;无”;在张量流中,python,tensorflow,neural-network,Python,Tensorflow,Neural Network,在图形构建阶段,假设张量x,这是一个神经网络的完全连接层 因此假设x的形状是(?,5)。我想在python中设置最后一列,如下所示: for i in range(x.shape[0]): if x[i,-1] < 0.5: x[i,-1] = 0.0 else: x[i,-1] = 1.0 当x的第一个维度是??这样时,我如何检查上面的内容 import tensorflow as tf import numpy as np a = t
x
,这是一个神经网络的完全连接层
因此假设x
的形状是(?,5)
。我想在python中设置最后一列,如下所示:
for i in range(x.shape[0]):
if x[i,-1] < 0.5:
x[i,-1] = 0.0
else:
x[i,-1] = 1.0
当x
的第一个维度是?
?这样时,我如何检查上面的内容
import tensorflow as tf
import numpy as np
a = tf.placeholder(tf.int32, shape=[None, 5])
r, c = a.get_shape()
x_split = tf.split(1, c, a) # split a along axis 1
last_col = x_split[-1]
mask = tf.greater(last_col, tf.constant(6))
cond = tf.where(mask,
tf.add(last_col, tf.constant(1)), # if true, add one
tf.add(last_col, tf.constant(-1)))# if false, minus one
x_split = x_split[0:-1]
x_split.append(cond)
ans = tf.concat(1, x_split)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
arr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]])
the_ans = sess.run(ans, feed_dict={a: arr})
'''
the_ans is
[[ 1 2 3 4 4]
[ 6 7 8 9 11]]
'''
像这样
import tensorflow as tf
import numpy as np
a = tf.placeholder(tf.int32, shape=[None, 5])
r, c = a.get_shape()
x_split = tf.split(1, c, a) # split a along axis 1
last_col = x_split[-1]
mask = tf.greater(last_col, tf.constant(6))
cond = tf.where(mask,
tf.add(last_col, tf.constant(1)), # if true, add one
tf.add(last_col, tf.constant(-1)))# if false, minus one
x_split = x_split[0:-1]
x_split.append(cond)
ans = tf.concat(1, x_split)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
arr = np.array([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]])
the_ans = sess.run(ans, feed_dict={a: arr})
'''
the_ans is
[[ 1 2 3 4 4]
[ 6 7 8 9 11]]
'''
非常感谢你的回答,这是我想要的:-)非常感谢你的回答,这是我想要的:-)