繁体   English   中英

logits 和标签必须是可广播的:logits_size=[100,4] labels_size=[400,4]

[英]logits and labels must be broadcastable: logits_size=[100,4] labels_size=[400,4]

我的康达环境

Python: 3.6.10
NumPy: 1.18.1
Pandas: 0.23.4
TensorFlow: 2.2.0

我正在使用自定义生成器并训练 CNN 1D。

生成器将文件名列表和批量大小作为输入,并从文件名中输出数据和 one-hot 编码标签。

数据

数据在多个文件中。 每个文件在 10 列中有 10 个长度为 8001 的单独信号。

自定义生成器批量加载文件

def tf_data_generator(file_list, batch_size = 10):
    i = 0
    while True:
        if i*batch_size >= len(file_list):  
            i = 0
            np.random.shuffle(file_list)
        else:
            file_chunk = file_list[i*batch_size:(i+1)*batch_size] 
            data = []
            labels = []
            label_classes = tf.constant(["RS0", "Td2", "Jum","Td3"]) 
            for file in file_chunk:
                temp = pd.read_csv(open(file,'r'),header=None,index_col=False,squeeze=True)#
                sigdf=temp.str.split(expand=True,)
                sigdf=sigdf.replace('NaN',np.nan)
                sigdf=sigdf.dropna(axis=1)

                data.append(sigdf.values.reshape(10,8001,1)) 
                pattern = tf.constant(eval("file[79:-4]")) #extracting data from file name
                for j in range(len(label_classes)):
                    if re.match(pattern.numpy(), label_classes[j].numpy()): # matching pattern
                        l=[j]*len(sigdf.columns) #repeating label outputs according to the number of cols 
                        labels.extend(l)
            data = np.asarray(data).reshape(-1,8001,1)
            #one hot encoding the data
            labels = np.asarray(utils.to_categorical(labels))
            yield data, labels
            i = i + 1

代码检查发电机 Output

num = 0
for data, labels in check_data:
    print(data.shape, labels.shape)
    #print(labels, "<--Labels")
    print()
    num = num + 1
    if num > 5: break

Output

(100, 8001, 1) (100, 4)

(100, 8001, 1) (100, 4)

(100, 8001, 1) (100, 4)

(100, 8001, 1) (100, 4)

(100, 8001, 1) (100, 4)

(100, 8001, 1) (100, 4)

此处显示 label 输出为100,4 ,用于 100 个信号,具有 4 个输出的单热编码。

CNN Model

model = tf.keras.Sequential([
    layers.Conv1D(16, 3, activation = "relu", input_shape = (8001,1)),
    layers.MaxPool1D(2),
    layers.Dropout(0.5),
    layers.Conv1D(32, 7, activation = "relu"),
    layers.MaxPool1D(2),
    layers.Dropout(0.5),
    layers.Conv1D(64, 7,activation="relu"),
    layers.MaxPool1D(2),
    layers.Dropout(0.5),
    layers.Flatten(),
    layers.Dense(16, activation = "relu"),
    layers.Dense(4, activation = "softmax")
])

model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])

但是,当我尝试安装 model 时出现错误

InvalidArgumentError:  logits and labels must be broadcastable: logits_size=[100,4] labels_size=[400,4]
     [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at <ipython-input-105-142deaf73db1>:2) ]] [Op:__inference_train_function_26061]

在错误中,它显示[400,4]作为标签大小。 而生成器 output 显示[100,4] 我不确定我哪里出错了!

这可能是因为标签的尺寸与 output 的尺寸不同。 (参见categorical_crossentropy )。
要找出答案,您可以显示 model 的摘要以检查 output 形状,其中: model.summary() 或者您可以调用 model model(np.zeros((1, 8001, 1)))并检查张量的形状。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM