Python Tensorflow:“;不允许在“tf.Tensor”上迭代;在训练一个有多种输入的CNN时
我想制作一个CNN,它可以拍3张照片来做预测。在神经网络内部,将来自3个神经网络的预测连接起来。我很难给它正确的输入。可以轻松地复制/粘贴并运行该示例 问题出现在Python Tensorflow:“;不允许在“tf.Tensor”上迭代;在训练一个有多种输入的CNN时,python,numpy,tensorflow,machine-learning,keras,Python,Numpy,Tensorflow,Machine Learning,Keras,我想制作一个CNN,它可以拍3张照片来做预测。在神经网络内部,将来自3个神经网络的预测连接起来。我很难给它正确的输入。可以轻松地复制/粘贴并运行该示例 问题出现在call()方法之前,我试图将输入分离开来,将它们发送到不同的神经网络中。我尝试了多个作业,我尝试了,zip(),等等 import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf from tensorflow import keras as K
call()
方法之前,我试图将输入分离开来,将它们发送到不同的神经网络中。我尝试了多个作业,我尝试了,zip()
,等等
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
from tensorflow import keras as K
import numpy as np
from functools import partial
mnist = K.datasets.cifar10.load_data()
(xtrain, ytrain), (xtest, ytest) = mnist
train_indices = np.random.randint(0, 50_000, (100_000, 3))
test_indices = np.random.randint(0, 10_000, (20_000, 3))
train_inputs = xtrain[train_indices].astype(np.float32)/255
test_inputs = xtest[test_indices].astype(np.float32)/255
train_outputs = np.array(np.sum(ytrain[train_indices], axis=1) % 2 == 0, dtype=np.int32)
test_outputs = np.array(np.sum(ytest[test_indices], axis=1) % 2 == 0, dtype=np.int32)
x = tf.data.Dataset.from_tensor_slices(train_inputs).map(lambda x: tf.expand_dims(x, 1))
y = tf.data.Dataset.from_tensor_slices(train_outputs)
train_ds = tf.data.Dataset.zip((x, y))
test_ds = tf.data.Dataset.from_tensor_slices((test_inputs, test_outputs))
class MultiInputCNN(K.Model):
def __init__(self):
super(MultiInputCNN, self).__init__()
custom_net = partial(K.applications.MobileNetV2,
input_shape=(32, 32, 3),
include_top=False,
weights=None)
self.net1 = custom_net()
self.net2 = custom_net()
self.net3 = custom_net()
self.concat = K.layers.Concatenate()
self.pool = K.layers.GlobalAveragePooling2D()
self.dropout = K.layers.Dropout(.5)
self.dense = K.layers.Dense(2)
def call(self, inputs, training=None, **kwargs):
x, y, z = inputs[0]
a = self.net1(x)
b = self.net2(y)
c = self.net3(z)
x = self.concat([a, b, c])
x = self.pool(x)
x = self.dropout(x)
x = tf.nn.sigmoid(self.dense(x))
return x
model = MultiInputCNN()
model(next(iter(train_ds)))
OperatorNotAllowedInGraphError:不允许在tf上迭代。Tensor
:AutoGraph未转换此函数。尝试直接用@tf.function来装饰它
这是我的解决方案。。。如果我理解正确,有3个modilenet(不是1个共享权重,在这种情况下,下面的代码很容易修改)
这里是运行示例:(我减少n_样本以不耗尽ram)这里是我的解决方案。。。如果我理解正确,有3个modilenet(不是1个共享权重,在这种情况下,下面的代码很容易修改)
这里是运行示例:(我减少了n_sample,使其不耗尽ram)因此错误在于移动网络层指向同一个对象?我认为这不是错误,因为在我的本地机器上也可以工作。但是我更喜欢在colab上测试它,它不工作,所以我用上面的格式重写了解决方案,它没有问题。错误是移动网络层指向同一个对象?我不认为这是一个错误,因为在我的本地机器上也工作。但是我更喜欢在colab上测试它,它不工作,所以我用上面的格式重写了解决方案,它没有问题
class MultiInputCNN(K.Model):
def __init__(self):
super(MultiInputCNN, self).__init__()
self.custom_net1 = K.applications.MobileNetV2(
input_shape=(32, 32, 3),
include_top=False,
weights=None)
self.custom_net2 = K.applications.MobileNetV2(
input_shape=(32, 32, 3),
include_top=False,
weights=None)
self.custom_net3 = K.applications.MobileNetV2(
input_shape=(32, 32, 3),
include_top=False,
weights=None)
self.concat = K.layers.Concatenate()
self.pool = K.layers.GlobalAveragePooling2D()
self.dropout = K.layers.Dropout(.5)
self.dense = K.layers.Dense(2)
def call(self, inputs, training=None, **kwargs):
x, y, z = inputs[0]
net1 = self.custom_net1(x)
net2 = self.custom_net2(y)
net3 = self.custom_net3(z)
x = self.concat([net1, net2, net3])
x = self.pool(x)
x = self.dropout(x)
x = tf.nn.sigmoid(self.dense(x))
return x
model = MultiInputCNN()
model(next(iter(train_ds)))