

kerasで2クラスの分類をCNNで行おうと思っています。以下がコードです

[code]

    import random
    import pandas as pd
    from pandas import Series,DataFrame
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    import keras
    from keras.datasets import fashion_mnist
    from keras.models import Sequential
    from keras.layers import Dense, Dropout,Activation ,Flatten, Dropout, 
    Conv2D, MaxPooling2D, Reshape
    from keras.optimizers import RMSprop
    from keras.optimizers import Adam
    
    df_list = []
    for i in range(4):    
        cl_data_set = pd.read_csv("./csv/cl_" + str(i) +".csv",sep=",",header=0)
        cl_x = DataFrame(cl_data_set.drop("POINT",axis=1))
        df_list.append(cl_x)
        op_data_set = pd.read_csv("./csv/op_" + str(i) +".csv",sep=",",header=0)
        op_x = DataFrame(op_data_set.drop("POINT",axis=1))
        df_list.append(op_x)
    
    close_1 = np.array(df_list[0])
    open_1 = np.array(df_list[1])
    close_2 = np.array(df_list[2])
    open_2 = np.array(df_list[3])
    close_3 = np.array(df_list[4])
    open_3 = np.array(df_list[5])
    close_4 = np.array(df_list[6])
    open_4 = np.array(df_list[7])
    train_dataset = []
    test_dataset = []
    for i in range(0,13000,100):
        if i >= 10400:
            test_dataset.append([close_1[i:i+100,:],1])
            test_dataset.append([close_2[i:i+100,:],1])
            test_dataset.append([open_1[i:i+100,:],0])
            test_dataset.append([open_2[i:i+100,:],0])
    
        else:
            train_dataset.append([close_1[i:i+100,:],1])
            train_dataset.append([close_2[i:i+100,:],1])
            train_dataset.append([open_1[i:i+100,:],0])
            train_dataset.append([open_2[i:i+100,:],0])
    
    random.shuffle(train_dataset)
    random.shuffle(test_dataset)
    
    x_train_list = []
    y_train_list = []
    x_test_list = []
    y_test_list = []
    for i in range(416):
            x_train_list.append(train_dataset[i][0])
            y_train_list.append(train_dataset[i][1])
    
    for i in range(104):
            x_test_list.append(test_dataset[i][0])
            y_test_list.append(test_dataset[i][1])
    
    x_train = np.array(x_train_list)
    x_test = np.array(x_test_list)
    y_train = np.array(y_train_list)
    y_test = np.array(y_test_list)
    model = Sequential()
    
    x_train = np.reshape(x_train,(-1,416,100,4))
    model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(416,100,4)))
    model.add(MaxPooling2D(pool_size=(1,2)))
    
    model.add(Dropout(0.4))
    model.add(Flatten())
    model.add(Dense(128, activation='sigmoid'))
    model.add(Dropout(0.4))
    model.add(Dense(1, activation='sigmoid'))
    
    model.summary()
    print("\n")
    model.compile(loss='binary_crossentropy',optimizer="Adam",metrics=['accuracy'])
    
    
    
    history = model.fit(x_train, y_train,batch_size=200,epochs=2000,verbose=1,validation_data=(x_test, y_test))
    
    
    score = model.evaluate(x_test,y_test,verbose=1)
    print("\n")
    print("Test loss:",score[0])
    print("Test accuracy:",score[1])
    
[/code]

次に、エラーの全リストを載せます

[code]

    Using TensorFlow backend.
    x_train.shape
    (416, 100, 4)
    x_test.shape
    (104, 100, 4)
    y_train.shape
    (416,)
    y_test.shape
    (104,)
    
    Traceback (most recent call last):
      File "CNN.py", line 121, in <module>
    history = model.fit(x_train, y_train,batch_size=200,epochs=2000,verbose=1,validation_data=(x_test, y_test))
      File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 950, in fit
    batch_size=batch_size)
      File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 802, in _standardize_user_data
    check_array_length_consistency(x, y, sample_weights)
      File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 236, in check_array_length_consistency
    'and ' + str(list(set_y)[0]) + ' target samples.')
    ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 416 target samples.
    
[/code]

サンプル数は全体の520個をtrain:test=8:2で分割しており、  
xは100pointずつ分割された4chのデータを、yはラベル(0か1)を持っています。  
エラーの解決策と必要な修正のご協力をお願いします


CC BY-SA 4.0
2019-01-04T08:54:18.590
51699
2023-09-22T09:00:55.110
2019-01-04T11:34:47.727
31667
31667
question
python numpy array 深層学習 keras
kerasのCNNで入力配列とターゲット(教師)配列が一致しない


x_train同様に、y_trainもデータ数の次元を作成すると直るのではないでしょうか。

[code]

    x_train = np.reshape(x_train,(-1,416,100,4)) # => この行と同様の処理をy_trainにも実施する：
    y_train = np.reshape(y_train,(-1,416))
    
[/code]


CC BY-SA 4.0
2019-01-06T05:34:36.993
51750
2019-01-06T05:34:36.993
26558
51699
answer