【问题标题】:Keras CNN training: Cannot use nested list as InputKeras CNN 训练:不能使用嵌套列表作为输入
【发布时间】:2020-07-19 18:05:38
【问题描述】:

(已编辑以包含数据集和模型代码)

我正在训练 Keras CNN 2d 矩阵。我正在创建自己的训练数据集,其中每个矩阵单元的形状为[[list], int]。单元格的第一个列表项是我转换为列表的字符串类的乘积(使用tf.keras.utils.to_categorical):

cell[0] = to_categorical(
                rnd_type-1, num_classes=num_types)

第二个是简单的int:

cell[1] = random.randint(0, max_val)

数据集创建函数:

def make_data(num_of_samples, num_types, max_height, grid_x, grid_y):
grids_list = []
target_list = []
target = 0
for _ in range(num_of_samples):
    # create empty grid 
    grid = [[[[],0] for i in range(grid_y)] for j in range(grid_x)]
    for i in range(grid_x):
        for j in range(grid_y):
            rnd_type = random.randint(
                0, num_types)
            # get random class 
            # and convert to cat list 
            cat = to_categorical(
                rnd_type-1, num_classes=num_types)
            # get random type 
            rnd_height = random.randint(0, max_height)
            # inject the two values into the cell 
            grid[i][j] = [cat, rnd_height]
            # get some target value 
            target += rnd_type * 5 + random.random()*5
    target_list.append(target)
    grids_list.append(grid)
    # make np arrs out of the lists 
    t = np.array(target_list)
    g = np.array(grids_list)
return t, g

我的模型是使用 model = models.create_cnn(grid_size, grid_size, 2, regress=True) 创建的,其中(我假设)Input 深度为 2。

模型创建代码:

num_types = 20
max_height = 50
num_of_samples = 10
grid_size = 10
epochs = 5000


# get n results of X x Y grid with target
targets_list, grids_list = datasets.make_data(
    num_of_samples, num_types, max_height, grid_size, grid_size)
    
split = train_test_split(targets_list, grids_list,
                         test_size=0.25, random_state=42)
(train_attr_X, test_attr_X, train_grids_X, test_grids_X) = split

# find the largest value in the training set and use it to
# scale values to the range [0, 1]

max_target = train_attr_X.max()
train_attr_Y = train_attr_X / max_target
test_attr_Y = test_attr_X / max_target

model = models.create_cnn(grid_size, grid_size, 2, regress=True)

但是,鉴于此错误,我无法对其进行训练:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).

【问题讨论】:

  • 请向我们展示您的代码

标签: python tensorflow keras input


【解决方案1】:

回答我自己的问题:

model 只能接受 int 作为深度。因此,我的矩阵的深度必须是 int len 的列表,而不是二维矩阵。因此,将类数据与连续字段rnd_height合并的方式是:

  • class=> cat = to_categorical
  • cell = np.append(cat, [rnd_height])

这样,cat 列表添加了 rnd_height 值。 整个数据集函数现在看起来像这样:

def make_data(num_of_samples, num_types, max_height, grid_x, grid_y):
    grids_list = []
    target_list = []
    target = 0
    for _ in range(num_of_samples):
        grid = [[[False, False] for i in range(grid_y)] for j in range(grid_x)]
        for i in range(grid_x):
            for j in range(grid_y):
                rnd_type = random.randint(
                    0, num_types)
                cat = to_categorical(
                    rnd_type-1, num_classes=num_types)
                rnd_height = random.randint(0, max_height)
                cell = np.append(cat, [rnd_height])
                grid[i][j] = cell

                # simulate simple objective function
                if rnd_type < num_types/5:
                    target += rnd_height * 5

        target_list.append(target)
        grids_list.append(grid)

        t = np.array(target_list)
        g = np.array(grids_list)
    # return grids and targets
    return g, t

【讨论】:

    猜你喜欢
    • 2017-07-07
    • 2019-02-12
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多