8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

层“模型”需要 2 个输入,但实际收到 1 个输入张量。输入记录:[ ]

Shyam Patel 2月前

327 0

`我正在将 tensorflow 2.4.1 升级到 2.15.0。我正在重新使用用 2.4.1 训练的模型。我无法在升级版本中进行批量预测。输入格式为 1 tensor.Model

`我正在将 tensorflow 2.4.1 升级为 2.15.0。我正在重新使用用 2.4.1 训练的模型。我无法在升级版本中进行批量预测。输入格式为 1 张量。模型输入形状:[(None, 46, 1), (None, 46, 1)]批量数据形状:[(200, 46, 1), (200, 46, 1)]

两个版本的模型摘要相同。

import tensorflow as tf
import numpy as np

class predictDataGenerator(tf.keras.utils.Sequence):
    'Generates data for Keras'
    def __init__(self, inData, nsamps, batch_size=250, dim=46,
                 n_channels=1, inData_ch2=None, n_classes=2,
                 avg_num=3, befN=18, shuffle=True):
        'Initialization'
        self.dim = dim
        self.batch_size = batch_size
        self.inData = inData
        self.inData_ch2 = inData_ch2
        self.n_channels = n_channels
        self.n_classes = n_classes
        self.avg_num = avg_num
        self.befN = befN
        self.part_start = (self.befN + 1) * self.avg_num + self.avg_num // 2 - (self.befN + 1)
        self.part_window = self.dim + 1
        self.obj_window = (self.dim + 1) * self.avg_num
        self.part_tile = np.tile(np.arange(self.dim), (self.batch_size, 1))
        self.part_tile += np.arange(self.batch_size).reshape(self.batch_size, 1)
        self.shuffle = shuffle
        self.nsamps = nsamps
        self.on_epoch_end()
        self.batch_count = self.__len__()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return int(np.ceil(self.nsamps / self.batch_size))

    def __getitem__(self, index):
        'Generate one batch of data'
        # Generate indexes of the batch
        batch_data = self.inData[index * self.batch_size:(index + 1) * self.batch_size + self.obj_window - 1]
        num_points = batch_data.size - (self.obj_window - 1)
        X_OBJ = np.empty((num_points, self.dim, self.n_channels))
        X_PART = np.empty((num_points, self.dim, self.n_channels))
        for i in range(self.n_channels):
            X_obj, X_part = self.__data_generation(batch_data, num_points)
            X_PART[:, :, i] = X_part
            X_OBJ[:, :, i] = X_obj
        return (X_OBJ, X_PART)

    def on_epoch_end(self):
        'Updates indexes after each epoch'
        # Optional: Implement shuffling if needed
        if self.shuffle:
            self.indexes = np.arange(self.nsamps)
            np.random.shuffle(self.indexes)

    def get_part_data(self, batch_data, num_points):
        'Prepare part data'
        part_end = self.part_start + num_points + self.part_window
        temp_data = batch_data[self.part_start:part_end]  # Required samples
        temp_data = np.diff(temp_data)  # temp_data[1:] - temp_data[:-1]
        X_part2 = temp_data[self.part_tile[:num_points, :]]
        return X_part2

    def get_obj_data(self, batch_data, num_points):
        'Prepare object data'
        X_OBJ2 = np.empty((num_points, self.dim))
        for i in range(self.avg_num):
            temp_end = batch_data.size - (batch_data.size - i) % self.avg_num
            temp_data = batch_data[i:temp_end]
            temp_data = temp_data.reshape(-1, self.avg_num)
            temp_data = np.sum(temp_data, axis=1) / self.avg_num
            temp_data = np.diff(temp_data)
            num_windows = temp_data.size - self.dim + 1
            obj_tile = np.tile(np.arange(self.dim), (num_windows, 1))
            obj_tile += np.arange(num_windows).reshape(num_windows, 1)
            X_OBJ2[i::self.avg_num] = temp_data[obj_tile]
        return X_OBJ2

    def __data_generation(self, batch_data, num_points):
        'Generates data for one batch'
        X_part = self.get_part_data(batch_data, num_points)
        X_obj = self.get_obj_data(batch_data, num_points)
        X_part = X_part - np.mean(X_part, axis=1).reshape(-1, 1)
        X_part = X_part / ((np.std(X_part, axis=1)).reshape(-1, 1) + 1e-100)
        X_obj = X_obj - np.mean(X_obj, axis=1).reshape(-1, 1)
        X_obj = X_obj / ((np.std(X_obj, axis=1)).reshape(-1, 1) + 1e-100)
        return X_obj, X_part

X_test = np.arange(1140)
pred_length = X_test.size-l_start-l_end
bsize = 200 

testgen = predictDataGenerator(X_test.reshape(-1),
                    pred_length,batch_size=bsize,dim = Window,
                    avg_num = avgN,
                    befN = window1,shuffle=False)
print(len(testgen))
probs = bd_model.predict(testgen)

错误:

ValueError:层“模型”需要 2 个输入,但实际收到 1 个输入张量。收到的输入:[ ]`

帖子版权声明 1、本帖标题:层“模型”需要 2 个输入,但实际收到 1 个输入张量。输入记录:[ ]
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Shyam Patel在本站《tensorflow》版块原创发布, 转载请注明出处!
最新回复 (0)
  • @Alanwagner 请更新您的答案,以包括类似 Jeyan 的区域评论。或者 Jeyan,请您发布您的答案,它将被接受为答案

返回
作者最近主题: