Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将随机张量与标量相乘?_Python_Tensorflow - Fatal编程技术网

Python 如何将随机张量与标量相乘?

Python 如何将随机张量与标量相乘?,python,tensorflow,Python,Tensorflow,我正在做: with sess.as_default(): inputs = tf.random.uniform( shape=[10], minval=-1, maxval=1, dtype=tf.dtypes.float32 ) outputs = inputs * 2 return inputs, outputs 但是,

我正在做:

    with sess.as_default():
        inputs = tf.random.uniform(
            shape=[10],
            minval=-1,
            maxval=1,
            dtype=tf.dtypes.float32
        )

        outputs = inputs * 2
        return inputs, outputs
但是,这给了我一些不正确的值:

[-0.35871983  0.38419914  0.4842844   0.73863363  0.9198251   0.872777
 -0.02426648 -0.23505187  0.12343764  0.98620296]

[-1.5875583  -0.20895815 -0.38869858 -0.22130203  0.00478601  0.06755686
 -1.2828503   0.13379574  0.91710424  1.2863417 ]
第二个张量应该是第一个张量的2倍


我使用的是Tensorflow 1.15.0,如果这很重要的话

您的代码不能以当前形式运行。看起来您给出了函数的一部分,但没有给出
def
语句。无论如何,这里是1.15.0中的一个工作示例

将tensorflow导入为tf
输入=tf.random.uniform(
形状=[10],
minval=-1,
maxval=1,
dtype=tf.dtypes.float32
)
输出=输入*2
将tf.Session()作为s:
out=s.run([输入,输出])
out
的结果为

[array([ 0.39246178,  0.77169394,  0.05202556,  0.979944  , -0.9819634 ,
        -0.56705594,  0.64549136,  0.59383535, -0.5887065 ,  0.90850115],
       dtype=float32),
 array([ 0.78492355,  1.5433879 ,  0.10405111,  1.959888  , -1.9639268 ,
        -1.1341119 ,  1.2909827 ,  1.1876707 , -1.177413  ,  1.8170023 ],
       dtype=float32)]

tensorflow 2.x方式:

您使用的是哪个版本的tensorflow?使用tensorflow 2.1.0,结果会加倍。虽然我没有显式地使用会话(TensorFlow2.x没有这样做)

输出是

(<tf.Tensor: shape=(10,), dtype=float32, numpy=
 array([-0.02820992,  0.50165296, -0.8978882 ,  0.28159666,  0.00776339,
         0.8920951 ,  0.89258003, -0.25228214, -0.25257993, -0.32589626],
       dtype=float32)>,
 <tf.Tensor: shape=(10,), dtype=float32, numpy=
 array([-0.05641985,  1.0033059 , -1.7957764 ,  0.5631933 ,  0.01552677,
         1.7841902 ,  1.7851601 , -0.5045643 , -0.50515985, -0.6517925 ],
       dtype=float32)>)
(,
)
(<tf.Tensor: shape=(10,), dtype=float32, numpy=
 array([-0.02820992,  0.50165296, -0.8978882 ,  0.28159666,  0.00776339,
         0.8920951 ,  0.89258003, -0.25228214, -0.25257993, -0.32589626],
       dtype=float32)>,
 <tf.Tensor: shape=(10,), dtype=float32, numpy=
 array([-0.05641985,  1.0033059 , -1.7957764 ,  0.5631933 ,  0.01552677,
         1.7841902 ,  1.7851601 , -0.5045643 , -0.50515985, -0.6517925 ],
       dtype=float32)>)