gpt4 book ai didi

python - 为什么对张量的多次调用每次使用 reshape 都会更改其值

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:56 25 4
gpt4 key购买 nike

我试图在 Jupyter Notebook 中使用 Tensorflow 调试一些代码,并注意到了我没有预料到的行为。我注意到下面所有打印语句的输出都会为 a_S 生成不同的输出矩阵。为什么会这样?我本以为 a_S = tf.random... 行会被计算一次。然后 reshape 运行。由于 reshape 已经发生,第二次和第三次调用不会改变吗?另外,该行中没有任何内容会导致 a_S 更改值,除非它在每次打印时以某种方式重新分配?

with tf.Session() as test:
tf.set_random_seed(1)
a_S = tf.random_normal([1, 4, 7, 3], mean=1, stddev=4)
a_G = tf.random_normal([1, 4, 7, 3], mean=1, stddev=4)
J_style_layer = compute_layer_style_cost(a_S, a_G)

m, n_H, n_W, n_C = a_G.get_shape().as_list()

#print("J_style_layer = " + str(J_style_layer.eval()))
print("J_style_layer2 = " + str(tf.reshape(a_S,[-1,n_C]).eval()))
print("J_style_layer3 = " + str(tf.reshape(a_S,[-1,n_C]).eval()))
print("J_style_layer3 = " + str(tf.reshape(a_S,[-1,n_C]).eval()))

输出:

J_style_layer2 = [[ -1.68344498e+00   1.89428568e+00   4.18909216e+00]
[ 1.38468242e+00 3.89259148e+00 2.35248661e+00]
[ -1.92024541e+00 4.64613724e+00 -1.03752756e+00]
[ 4.89945602e+00 -7.53608036e+00 3.40916491e+00]
[ -2.48588562e-01 -2.43247509e+00 8.14658546e+00]
[ -1.76469064e+00 2.46943331e+00 -6.58598471e+00]
[ 7.14989901e-01 6.61043763e-01 4.19998550e+00]
[ 2.50623369e+00 4.50162363e+00 1.56363678e+00]
[ 3.42334247e+00 -8.45517635e+00 3.44382668e+00]
[ 1.22528923e+00 2.32022464e-01 1.46155047e+00]
[ 4.21129417e+00 -4.74298620e+00 -2.33842206e+00]
[ 1.37766552e+00 -3.39658976e+00 2.58650005e-01]
[ 1.25126350e+00 7.47679806e+00 5.12217665e+00]
[ 1.06175241e+01 -1.33930969e+00 9.90524054e-01]
[ -5.41032219e+00 -5.45012522e+00 -1.77322006e+00]
[ -4.09782457e+00 4.84395790e+00 2.74499822e+00]
[ -9.30834293e-01 4.23307610e+00 5.83413506e+00]
[ -2.86698842e+00 -5.36654806e+00 5.01119232e+00]
[ 2.98037910e+00 4.92405033e+00 -5.25441885e-01]
[ -2.72941589e-03 6.32100248e+00 1.18444359e+00]
[ 2.83025682e-01 -4.18499231e+00 1.21345019e+00]
[ -2.40400195e+00 -9.58013535e-01 7.72542524e+00]
[ 7.56009007e+00 5.49445009e+00 1.37617004e+00]
[ 5.44784927e+00 5.19741154e+00 4.22853470e+00]
[ 6.33485937e+00 1.05327117e+00 6.67146921e+00]
[ -7.39221454e-01 3.46544337e+00 -1.65367699e+00]
[ 2.98967314e+00 1.79384637e+00 -1.96472073e+00]
[ 4.62664247e-01 -2.60094047e+00 4.77080643e-01]]

J_style_layer3 = [[ -0.96795654 3.94465876 -3.58533096]
[ 2.86657572 2.92028189 0.78808147]
[ 5.67818737 7.19356251 1.75864506]
[ -2.02141023 0.41648489 -3.99681711]
[ 6.6851511 -4.29682779 -3.34400749]
[ 4.41373968 -0.86847067 5.63204002]
[ -9.63679504 8.1419487 3.75494003]
[ -0.99762154 2.573452 4.2510972 ]
[ -1.79123497 -4.25948954 0.93429548]
[ -3.26150131 -1.08424997 -2.17739081]
[ 5.18679285 5.10724735 -1.09539294]
[ 4.88294411 -3.20969486 8.78791428]
[ -0.57771873 -2.99525881 2.32472777]
[ 1.36514819 2.2696619 2.3027463 ]
[ -0.12692893 5.43655586 3.8288033 ]
[ 8.78754711 1.60508752 -0.90078032]
[ 0.03909284 -1.96740341 3.91209292]
[ -2.23007846 -4.52376413 5.86214685]
[ -2.71961284 1.87416935 10.11991024]
[ 2.14405489 1.23315167 -0.92923737]
[ -4.21774721 1.86219811 0.18963808]
[ 5.43474102 4.82147026 0.60951805]
[ 5.42611742 1.51715541 4.34838676]
[ 1.46284723 6.85462999 3.54845214]
[ 5.46560287 2.08948946 -4.51927328]
[ 4.75431013 3.07936954 1.04333222]
[ 0.33259553 -0.8477515 -5.67703581]
[ -0.64754236 -1.51528716 1.61516106]]

J_style_layer3 = [[ 3.02279925 3.8460381 -3.10602283]
[ -2.568856 -3.49679995 4.63807583]
[ 0.43675953 3.30313778 -8.33617496]
[ -2.65553975 3.8253727 6.25587273]
[ 0.36330599 3.29871988 0.04714996]
[ 1.96590877 -1.24606895 -1.1522367 ]
[ 7.65323305 -2.6084044 0.94568378]
[ -6.0451417 0.61521298 -3.07440042]
[ 4.33242702 1.00051153 1.34616411]
[ 3.1335001 1.86272466 10.08991051]
[ 0.5303157 0.73839551 7.40760946]
[ -3.67107534 5.532269 -0.37785971]
[ -0.60100091 -6.75784874 5.94953918]
[ 4.20541143 -1.72344565 -1.09141064]
[ -5.15075636 -0.99294972 0.41196495]
[ -2.43932915 2.13546133 1.18984747]
[ -0.13836217 1.94724464 4.87366867]
[ -2.72181702 3.02790046 -13.79762745]
[ -1.3500247 3.25204515 -3.60114098]
[ 8.59435654 -0.15901434 -0.41931808]
[ 2.08263826 -1.55667186 3.37390661]
[ 2.86219215 -5.58194542 -9.734231 ]
[ 7.96397781 2.00432396 -6.02174616]
[ -5.0965724 2.81370831 -0.9835161 ]
[ 1.61186719 4.38890743 6.93289137]
[ 4.34721375 0.67843139 4.64758778]
[ -1.49379516 4.09007645 2.99832821]
[ -2.06486607 2.20908523 3.9281044 ]]```

最佳答案

当你写

a_S = tf.random_normal([1, 4, 7, 3], mean=1, stddev=4)

您正在做的是在图中创建一个节点, tensorflow 使用该节点来执行您告诉它的任何计算。在这种情况下,您创建的节点将生成随机张量。这与 reshape 功能没有任何关系。您已经创建了一个节点,在评估时会返回一个新的随机张量。您可以看到一个最小的示例,如下所示:

import tensorflow as tf


with tf.Session() as test:
tf.set_random_seed(1)
a_S = tf.random_normal([1])
print("a " + str(a_S.eval()))
print("b " + str(a_S.eval()))

打印内容

a [-0.67086124]
b [0.9259123]

编辑例如,如果你用它做一些更复杂的事情

import tensorflow as tf


with tf.Session() as test:
tf.set_random_seed(1)
a_S = tf.random_normal([1])

# print statement triggered whenever we go through node b_S
a_S = tf.Print(a_S, [], message='We went through a_S', summarize=100)

b_S = 2*a_S
b_S = tf.Print(b_S, [], message='We went through b_S', summarize=100)

a_S.eval()
print('___________')
b_S.eval()
# We went through a_S
# ___________
# We went through a_S
# We went through b_S

那么 TF 将只运行它需要的内容来评估您所要求的内容。评估图表某些部分的唯一内容是 .evalsession.run(..)。其他一切都是向图中添加东西,无论是张量还是操作。

关于python - 为什么对张量的多次调用每次使用 reshape 都会更改其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56080960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com