A_0xl 2021-07-14 20:42 采纳率: 33.3%
浏览 724

报错: logits and labels must be broadcastable

python-tensorflow-keras-CNN
代码是轴承竞赛的,只改了一点,输出层4个神经元。报错也不清楚需要修改哪里

import keras  
from scipy.io import loadmat  
import matplotlib.pyplot as plt  
import glob  
import numpy as np  
import pandas as pd  
import math  
import os  
from keras.layers import *  
from keras.models import *  
from keras.optimizers import *  
import numpy as np  

MANIFEST_DIR = r"D:\\ChangZhou\Team\Python_study\Date\traingz.csv"  
Batch_size = 30  
Long = 800  
Lens = 560  

#把标签转成oneHot  
def convert2oneHot(index,Lens):  
    hot = np.zeros((Lens,))  
    hot[int(index)] = 1  
    return(hot) 

def xs_gen(path=MANIFEST_DIR,batch_size = Batch_size,train=True,Lens=Lens):  

    img_list = pd.read_csv(path)  
    if train:  
        img_list = np.array(img_list)[:Lens]  
        print("Found %s train items."%len(img_list))  
        print("list 1 is",img_list[0,-1])  
        steps = math.ceil(len(img_list) / batch_size)    # 确定每轮有多少个batch  
    else:  
        img_list = np.array(img_list)[Lens:]  
        print("Found %s test items."%len(img_list))  
        print("list 1 is",img_list[0,-1])  
        steps = math.ceil(len(img_list) / batch_size)    # 确定每轮有多少个batch  
    while True:  
        for i in range(steps):  

            batch_list = img_list[i * batch_size : i * batch_size + batch_size]  
            np.random.shuffle(batch_list)  
            batch_x = np.array([file for file in batch_list[:,1:-1]])  
            batch_y = np.array([convert2oneHot(label,10) for label in batch_list[:,-1]])  

            yield batch_x, batch_y  

TEST_MANIFEST_DIR = r"D:\\ChangZhou\Team\Python_study\Date\testgz.csv"

def ts_gen(path=TEST_MANIFEST_DIR,batch_size = Batch_size):  

    img_list = pd.read_csv(path)  

    img_list = np.array(img_list)[:Lens]  
    print("Found %s train items."%len(img_list))  
    print("list 1 is",img_list[0,-1])  
    steps = math.ceil(len(img_list) / batch_size)    # 确定每轮有多少个batch  
    while True:  
        for i in range(steps):  

            batch_list = img_list[i * batch_size : i * batch_size + batch_size]  
            #np.random.shuffle(batch_list)  
            batch_x = np.array([file for file in batch_list[:,1:]])  
            #batch_y = np.array([convert2oneHot(label,10) for label in batch_list[:,-1]])  

            yield batch_x
        
TIME_PERIODS = 5000

# 模型搭建
TIME_PERIODS = 5000  
def build_model(input_shape=(TIME_PERIODS,),num_classes=4):  
    model = Sequential()  
    model.add(Reshape((TIME_PERIODS, 1), input_shape=input_shape))  
    model.add(Conv1D(16, 8,strides=2, activation='relu',input_shape=(TIME_PERIODS,1)))  

    model.add(Conv1D(16, 8,strides=2, activation='relu',padding="same"))  
    model.add(MaxPooling1D(2))  

    model.add(Conv1D(64, 4,strides=2, activation='relu',padding="same"))  
    model.add(Conv1D(64, 4,strides=2, activation='relu',padding="same"))  
    model.add(MaxPooling1D(2))  
    model.add(Conv1D(256, 4,strides=2, activation='relu',padding="same"))  
    model.add(Conv1D(256, 4,strides=2, activation='relu',padding="same"))  
    model.add(MaxPooling1D(2))  
    model.add(Conv1D(512, 2,strides=1, activation='relu',padding="same"))  
    model.add(Conv1D(512, 2,strides=1, activation='relu',padding="same"))  
    model.add(MaxPooling1D(2))  

#  
    
    model.add(GlobalAveragePooling1D())  
    model.add(Dropout(0.3))  
    model.add(Dense(num_classes, activation='softmax'))  
    return(model)
print(model.summary()) 

Train = True


# 模型训练

Train = True  

if __name__ == "__main__": 
    if Train == True:  
        train_iter = xs_gen()  
        val_iter = xs_gen(train=False)  

        ckpt = keras.callbacks.ModelCheckpoint(  
            filepath='best_model.{epoch:02d}-{val_loss:.4f}.h5',  
            monitor='val_loss', save_best_only=True,verbose=1)  

        model = build_model()  
        opt = Adam(0.0002)  
        model.compile(loss='categorical_crossentropy',  
                    optimizer=opt, metrics=['accuracy'])  
        print(model.summary())  

        train_history=model.fit_generator(  
            generator=train_iter,  
            steps_per_epoch=Lens//Batch_size,  
            epochs=50,  
            initial_epoch=0,  
            validation_data = val_iter,  
            validation_steps = (Long - Lens)//Batch_size,     # ###gai
    
            callbacks=[ckpt],  
            )  
        model.save("finishModel.h5")  
    else:  
        test_iter = ts_gen()  
        model = load_model("best_model.49-0.00.h5")  
        pres = model.predict_generator(generator=test_iter,steps=math.ceil(528/Batch_size),verbose=1)  
        print(pres.shape)  
        ohpres = np.argmax(pres,axis=1)  
        print(ohpres.shape)  
        #img_list = pd.read_csv(TEST_MANIFEST_DIR)  
        df = pd.DataFrame()  
        df["id"] = np.arange(1,len(ohpres)+1)  
        df["label"] = ohpres  
        df.to_csv("predicts.csv",index=None)
        
        test_iter = ts_gen()
        for x in test_iter:
            x1 = x[0]
            break
        plt.plot(x1)
        plt.show()

然后报错:

InvalidArgumentError:  logits and labels must be broadcastable: logits_size=[30,4] labels_size=[30,10]
     [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at <ipython-input-40-bb346608d996>:28) ]] [Op:__inference_train_function_5286]

Function call stack:
train_function
  • 写回答

1条回答 默认 最新

  • 海洋 之心 2022年度博客之星人工智能领域TOP 1 2022-12-05 12:14
    关注

    你传入的输出结果与标签的维度不一致,导致算子无法计算损失值

    评论

报告相同问题?

问题事件

  • 创建了问题 7月14日

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题