如何将输入/输出传递到此网络?

时间:2017-04-03 07:58:21

标签: python-2.7 numpy keras conv-neural-network

我开始学习时似乎有些问题......我不确定为什么...... 网络是多输入(72个1d阵列),输出是1d阵列长度24. 1d阵列输出由与145个不同类相关的数字组成。

所以:72输入=> 24个输出

最小工作示例 - 未设置输入/输出。

import keras
from keras.utils import np_utils
from keras import metrics
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Lambda, Reshape,Flatten
from keras.layers import Conv1D,Conv2D, MaxPooling2D, MaxPooling1D, Reshape, ZeroPadding2D
from keras.utils import np_utils
from keras.layers.advanced_activations import LeakyReLU, PReLU
from keras.layers.advanced_activations import ELU
from keras.models import Model
from keras.layers import Input, Dense
from keras.layers import Dropout
from keras import backend as K
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import CSVLogger
from keras.callbacks import EarlyStopping
from keras.models import load_model
from keras.layers.merge import Concatenate
import numpy as np

def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]

nano_train_input = []
nano_train_output = []
nano_test_input = []
nano_test_output = []

##   Creating train input:
for i in range(974):
    nano_train_input.append(np.random.random((78,684,4)))
    nano_train_output.append(np.random.randint(145,size=(228)).tolist())

##   Creating test input:
for i in range(104):
    nano_test_input.append(np.random.random((78,684,4)))
    nano_test_output.append(np.random.randint(145,size=(228)).tolist())

def model(train_input, train_output, test_input, test_output, names=0):

    #   Paper uses dimension (40 x 45 =(15 * 3))
    #   Filter size 5
    #   Pooling size
    #   I use dimension (78  x 72 = (24 * 3)
    #   Filter size 9

    print "In model"
    i = 0
    print_once = True
    data_test_output = []
    data_test_input = []
    for matrix in test_input:
        row,col,channel = matrix.shape
        remove_output = (col/3)%24
        remove_input  = col%72
        if remove_output > 0 :
            test_output[i] = test_output[i][:-(remove_output)]
        for split in chunks(test_output[i],24):
            data_test_output.append(np.array(split))
        if remove_input > 0:
            out = np.split(matrix[:,:-(remove_input),:-1],matrix[:,:-(remove_input),:-1].shape[1]/72,axis=1)
        else:
            out = np.split(matrix[:,:,:-1],matrix[:,:,:-1].shape[1]/72,axis=1)
        data_test_input.extend(out)
        del out
        i=i+1 # Increment

    i=0
    data_train_output = []
    data_train_input = []
    for matrix in train_input:
        row,col,channel = matrix.shape
        remove_output = (col/3)%24
        remove_input  = col%72
        if remove_output > 0 :
            train_output[i] = train_output[i][:-(remove_output)]
        for split in chunks(train_output[i],24):
            data_train_output.append(np.array(split))
        if remove_input > 0:
            out = np.split(matrix[:,:-(remove_input),:-1],matrix[:,:-(remove_input),:-1].shape[1]/72,axis=1)
        else:
            out = np.split(matrix[:,:,:-1],matrix[:,:,:-1].shape[1]/72,axis=1)
        data_train_input.extend(out)
        del out
        i=i+1 # Increment

    print
    print "Len:"
    print len(data_train_input)
    print len(data_train_output)
    print len(data_test_input)
    print len(data_test_output)
    print
    print "Type[0]:"
    print type(data_train_input[0])
    print type(data_train_output[0])
    print type(data_test_input[0])
    print type(data_test_output[0])
    print
    print "Type:"
    print type(data_train_input)
    print type(data_train_output)
    print type(data_test_input)
    print type(data_test_output)
    print
    print "shape of [0]:"
    print data_train_input[0].shape
    print data_train_output[0].shape
    print data_test_input[0].shape
    print data_test_output[0].shape

    list_of_input = [Input(shape = (78,3)) for i in range(72)]
    list_of_conv_output = []
    list_of_max_out = []

    for i in range(72):
        list_of_conv_output.append(Conv1D(filters = 32 , kernel_size = 6 , padding = "same", activation = 'relu')(list_of_input[i]))
        list_of_max_out.append(MaxPooling1D(pool_size=3)(list_of_conv_output[i]))

    merge = keras.layers.concatenate(list_of_max_out)

    reshape = Flatten()(merge)

    dense1 = Dense(units = 500, activation = 'relu',    name = "dense_1")(reshape)
    dense2 = Dense(units = 250, activation = 'relu',    name = "dense_2")(dense1)
    dense3 = Dense(units = 24 , activation = 'softmax', name = "dense_3")(dense2)

    model = Model(inputs = list_of_input , outputs = dense3)
    model.compile(loss="categorical_crossentropy", optimizer="adam" , metrics = [metrics.sparse_categorical_accuracy])

    reduce_lr=ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, mode='auto', epsilon=0.01, cooldown=0, min_lr=0.000000000000000000001)
    stop  = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')

    print "Train!"

    print model.summary()


    hist_current = model.fit(x = ,
                        y = ,
                        shuffle=False,
                        validation_data=(,),
                        validation_split=0.1,
                        epochs=150000,
                        verbose=1,
                        callbacks=[reduce_lr,stop])

model(nano_train_input,nano_train_output,nano_test_input, nano_test_output)

输入和输出存储为numpy.ndarrays列表。 这是一个最小的工作示例..我应该如何将输入传递给输出?

2 个答案:

答案 0 :(得分:1)

如果你看看你的代码和输出,你确实有你说的话:24输出(data_train_outputs[0].shape)。但是,如果您查看Keras的图层输出,则将其作为输出:

dense_3 (Dense)                  (None, 26, 145)       36395                                        

我会说这应该是一个有形状的数组(无,24)....

我建议您添加一个重塑图层以获得您想要的输出!

答案 1 :(得分:1)

我会尝试:

Convolutional

您可能希望应用某些内容来转换 requestAjax = jQuery.ajax({ type: "POST", url: "ajax/ajax.salva-valutazione.php", data: {formdata:formdata, arrAccessori: arrAccessori}, // data:formdata, dataType: "json", success: function(risposta){ alert(".."+risposta); } }); 图层的输出。为了做到这一点 - 你需要压缩时间/顺序维度。为了做到这一点,尝试我提供的技术。