gpt4 book ai didi

python - 失败前提条件错误: Attempting to use uninitialized value conv2d_1/kernel with Tensorflow/Python

转载 作者:行者123 更新时间:2023-11-30 22:16:10 24 4
gpt4 key购买 nike

我正在尝试借助以下代码创建 CNN 模型:

import tensorflow as tf

class Create_CNN:
def _conv(self, input, nChannels, kernelSize, kernelStride):
conv = tf.layers.conv2d(inputs=input,
filters=nChannels,
kernel_size=kernelSize,
strides=(kernelStride, kernelStride),
padding='same',
activation=tf.nn.relu
)
return conv

def create_cnn(self, input, nChannels, kernelSize, kernelStride):
input = tf.reshape(input, shape=[-1, 500, 530, 3])
layer1 = Create_CNN()._conv(input, nChannels, kernelSize, kernelStride)
layer2 = Create_CNN()._conv(layer1, nChannels, kernelSize, kernelStride)
layer3 = Create_CNN()._conv(layer2, nChannels, kernelSize, kernelStride)
layer4 = Create_CNN()._conv(layer3, nChannels, kernelSize, kernelStride)
layer5 = Create_CNN()._conv(layer4, nChannels, kernelSize, kernelStride)
return layer5

在这里,我的输入是 500 * 530 * 3 维图像。我正在尝试使用以下代码传递输入和其他参数:

with tf.Session().as_default():
tf.global_variables_initializer().run()
i = PlantUtils().create_instance('ara2013_plant001_rgb.png', 'ara2013_plant001_label.png', 500, 530, 100, 106, 1, 1,
1)
input_image = i[0] # It is a 500 * 530 * 3 tensor
b = Create_CNN().create_cnn(input=input_image, kernelSize=3, kernelStride=1, nChannels=30)
x = tf.argmax(input=b, axis=1)
print x.eval()

当我尝试打印 logits(表示 x 的值)时,出现以下错误:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value conv2d_1/kernel

我不知道我做错了什么。我需要查看 CNN 模型生成的 logits。我真的需要这方面的帮助。

最佳答案

发生这种情况是因为您构建图表之前运行了初始化程序。理想情况下,您应该在创建Session之前构建Graph。试试这个

with tf.Graph().as_default():
i = PlantUtils().create_instance('ara2013_plant001_rgb.png', 'ara2013_plant001_label.png', 500, 530, 100, 106, 1, 1, 1)
input_image = i[0] # It is a 500 * 530 * 3 tensor
b = Create_CNN().create_cnn(input=input_image, kernelSize=3, kernelStride=1, nChannels=30)
x = tf.argmax(input=b, axis=1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(x)

关于python - 失败前提条件错误: Attempting to use uninitialized value conv2d_1/kernel with Tensorflow/Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50049485/

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