我正在尝试使用 tf 数据管道从几个文件中读取数据并训练多输入神经网络。我成功地训练了模型,但是当我想测试模型时,我得到了这个错误:

TypeError: Inputs to a layer should be tensors. Got: <PrefetchDataset shapes: (((None, None),,tensorflow,keras,deep-learning,tensorflow2.0"/>
	














首页 > 解决方案 > TypeError:层的输入应该是张量。得到:

我正在尝试使用 tf 数据管道从几个文件中读取数据并训练多输入神经网络。我成功地训练了模型,但是当我想测试模型时,我得到了这个错误:

TypeError: Inputs to a layer should be tensors. Got: <PrefetchDataset shapes: (((None, None),

问题描述

我正在尝试使用 tf 数据管道从几个文件中读取数据并训练多输入神经网络。我成功地训练了模型,但是当我想测试模型时,我得到了这个错误:

TypeError: Inputs to a layer should be tensors. Got: <PrefetchDataset shapes: (((None, None), (None, None)), (None,)), types: ((tf.float32, tf.float32), tf.float32)>

预处理功能:

def split_chr_by_tab(*lines):
    sep = ","
    features = [tf.strings.to_number(tf.strings.split(line, sep=sep), out_type=tf.float32) for line in lines]
    return tf.concat(features, axis=0)

def separate_input_output(input):
    return tuple([input[:-6], input[-6:-1]]), input[-1]

数据管道:

Train_dataset = tf.data.Dataset.list_files(Train_file_list)
Train_dataset= Train_dataset.shuffle(epoch).repeat(epoch).interleave(lambda filename: tf.data.TextLineDataset(filename).skip(1), num_parallel_calls=25)
Train_dataset= Train_dataset.map(split_chr_by_tab,num_parallel_calls=tf.data.experimental.AUTOTUNE).map(separate_input_output,num_parallel_calls=tf.data.experimental.AUTOTUNE)
Train_dataset= Train_dataset.batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

Test_dataset = tf.data.Dataset.list_files(Test_file_list)
Test_dataset= Test_dataset.interleave(lambda filename: tf.data.TextLineDataset(filename).skip(1), num_parallel_calls=25)
Test_dataset= Test_dataset.map(split_chr_by_tab,num_parallel_calls=tf.data.experimental.AUTOTUNE).map(separate_input_output,num_parallel_calls=tf.data.experimental.AUTOTUNE)
Test_dataset= Test_dataset.batch(batch_size).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

模型训练和评估(预测测试数据时出错):

inp1 = Input(shape=(1000, ),name="inp1")
inp2 = Input(shape=(4, ),name="inp2")
merge1=concatenate([inp1, inp2])
first_dense = Dense(32,kernel_initializer=initializer, )(merge1)
output=dense(1,)(first_dense)
model = Model(inputs=[inp1, inp2], outputs=output)
model.fit(Train_dataset,callbacks=callbacks_list,verbose=1,epochs=epoch,steps_per_epoch=10)
prediction = model(Test_dataset)

你能告诉我为什么我在预测过程中会出错吗?由于某些原因,我需要使用 model(Test_dataset) 而不是 model.predict(Test_dataset)。另外,如何存储预测值和真实标签?


证书有效

可能是这样;但它对“ec2-xxxx.us-east-2.compute.amazonaws.com”有效吗?你得到的错误基本上是说“我向 ec2-xxxx.us-east-2.compute.amazonaws.com 索要它的证书;它给了我一个有效的证书,但它的主机名不同(例如 bad.hacker.com)所以我不会相信它!”。

对于测试,您可以使用该tls_insecure_set()选项忽略它(请参阅文档)。但是,您确实需要查看证书并检查通用名称 (CN) 和主题备用名称 (SAN)(请参阅此答案)。

您在使用 IP 连接时得到的事实Error ! Result code 5使我怀疑证书是使用 IP 地址作为 SAN 创建的(因此当您使用 IP 连接时证书验证部分正在工作)。错误 5是“连接被拒绝,未授权”,因此您可能需要检查您的用户名/密码(和代理配置)。

标签: tensorflowkerasdeep-learningtensorflow2.0

解决方案


Test_dataset是一个PrefetchDataset对象,你不能将它传递给model.__call__,因为它需要一个张量作为输入。改用model.predict()

prediction = model.predict(Test_dataset)

如果您坚持使用调用模型进行预测,那么您可以这样使用它:

for p, _ in Test_dataset.take(1): # Takes 1 batch
    prediction = model(p)         # Predict 1 batch
print(prediction)

推荐阅读