python - 通过立即执行重新计算卷积值

标签 python tensorflow

我正在尝试使用急切执行。

我创建了一个训练集、一个权重和一个卷积层。

我声明卷积并更改权重。

如何在无需再次声明图层的情况下再次计算卷积?

我预计它会是这样的:

import tensorflow as tf


tf.enable_eager_execution()    
tfe = tf.contrib.eager


TrainingDataExample = tf.constant(0.5, shape=[8, 5, 6, 1], name="Inputs") 
WeightExample = tfe.Variable(tf.truncated_normal([1, 3, 1, 4], seed=1), name="Weights")
ConvExample = tf.nn.conv2d(TrainingDataExample, WeightExample, strides=[1, 1, 1, 1], padding="VALID", name="Conv")

NewWeightExample = tf.constant(2.0, shape=[1, 3, 1, 4], name="NewWeights")
WeightExample = tf.assign(WeightExample, NewWeightExample )
result = ConvExample 

print (result)

但是不行,卷积的值没有更新。

我该怎么做?

最佳答案

我不太明白你所说的“无需再次声明图层”是什么意思。

方法tf.nn.conv2d计算二维卷积。这就是如何使用 TensorFlow 计算卷积。

现在,您之前评估了 TrainingDataExample 上的卷积并将其存储在ConvExample中。通过生成一组新的权重,NewWeightExample并将其分配给覆盖变量 WeightExample它不会自动改变之前计算的卷积值ConvExample .

因此,您将必须使用新的权重集再次重新计算卷积。您只需重新运行即可做到这一点:

ConvExample = tf.nn.conv2d(TrainingDataExample, WeightExample,
                           strides=[1, 1, 1, 1], padding="VALID",
                           name="Conv")

得到以下结果:

<tf.Tensor: id=36, shape=(8, 5, 4, 4), dtype=float32, numpy=
array([[[[3., 3., 3., 3.],
         [3., 3., 3., 3.],
         [3., 3., 3., 3.],
         [3., 3., 3., 3.]],
...

关于python - 通过立即执行重新计算卷积值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52091047/

相关文章:

tensorflow - keras Conv2d值错误: Negative dimension size

python - Keras 中的分割网络在训练期间收敛到单个类

python - 一次加法需要多少 CPU 周期?

2 位数年份的 Python Epoch 失败

python - 如何在阅读后删除kafka消息

python - 深度学习: save and load a universal machine model through different libraries

javascript - 如何使用 Tensorflow.js 计算张量中每个值出现的次数?

python - 加入守护线程

python - 以下超链接和 "Filtered offsite request"

python - Tensorflow MNIST 教程 - 测试精度非常低