Beispiel #1
0
def signal_conv(inp):
    c1 = Conv1D(32, kernel_size=9, activation="relu", name="conv1d_1")(inp)
    norm1 = BatchNormalization(name="batch_norm_1")(c1)
    c2 = Conv1D(64, kernel_size=7, activation="relu", name="conv1d_2")(norm1)
    norm2 = BatchNormalization(name="batch_norm_2")(c2)

    ap1 = AvgPool1D(pool_size=2, strides=2, name="avg_pool_1")(norm2)

    c3 = Conv1D(64, kernel_size=7, activation="relu", name="conv1d_3")(ap1)
    norm3 = BatchNormalization(name="batch_norm_3")(c3)
    c4 = Conv1D(64, kernel_size=5, activation="relu", name="conv1d_4")(norm3)
    norm4 = BatchNormalization(name="batch_norm_4")(c4)

    ap2 = AvgPool1D(pool_size=2, strides=2, name="avg_pool_2")(norm4)

    c5 = Conv1D(128, kernel_size=5, activation="relu", name="conv1d_5")(ap2)
    norm5 = BatchNormalization(name="batch_norm_5")(c5)
    c6 = Conv1D(256, kernel_size=5, activation="relu", name="conv1d_6")(norm5)
    # norm6 = BatchNormalization(name="batch_norm_6")(c6)

    # mp3 = MaxPooling1D(pool_size=2, strides=2, name="max_pool_3")(norm6)
    #
    # c7 = Conv1D(256, kernel_size=11, activation="relu", name="conv1d_7")(mp3)
    # norm7 = BatchNormalization(name="batch_norm_7")(c7)
    # c8 = Conv1D(512, kernel_size=13, activation="relu", name="conv1d_8")(norm7)

    f = Flatten(name="flatten_signal")(c6)
    return f
Beispiel #2
0
def add_pooling_layer(model, pooling, pool_size, pool_stride):
    """

    Parameters
    ----------
    model: tf.keras.model
        Tensorflow.keras model to which to add the specified pooling layer

    pooling: str
        Pooling layer to add to the specified model

    pool_size: int
        Pooling size in the pooling layer

    Returns
    -------

    """
    if pooling == "max_pooling":
        model.add(MaxPool1D(pool_size=pool_size, strides=pool_stride))
    elif pooling == "avg_pooling":
        model.add(AvgPool1D(pool_size=pool_size, strides=pool_stride))
    else:
        print("Selected pooling is not available!")
def build_neural_network_model(Recurrent_Neural_Network: List[Any],
                               n_inputs: int, n_days: int) -> Sequential:
    """
    Builds neural net from config_neural_network_models.py
    Parameters
    ----------
    Recurrent_Neural_Network: List[Any]
        List of layers with parameters as a dictionary in the file
    n_inputs: int
        Number of days that will be fed into the NN
    n_days: int
        Number of days the NN wants to predict

    Returns
    -------
    model: Sequential
        Keras sequential model with layers from the file

    """
    model = Sequential()

    for idx_layer, d_layer in enumerate(Recurrent_Neural_Network):
        # Recurrent Neural Network
        if str(*d_layer) == "SimpleRNN":
            # Is this the input layer? If so, define input_shape
            if idx_layer == 0:
                model.add(
                    SimpleRNN(**d_layer["SimpleRNN"],
                              input_shape=(n_inputs, 1)))
            # Is this the last output layer? If so, set units to prediction days
            elif idx_layer == (len(Recurrent_Neural_Network) - 1):
                model.add(SimpleRNN(**d_layer["SimpleRNN"], units=n_days))
            else:
                model.add(SimpleRNN(**d_layer["SimpleRNN"]))

        # Long-Short Term-Memory
        elif str(*d_layer) == "LSTM":
            # Is this the input layer? If so, define input_shape
            if idx_layer == 0:
                model.add(LSTM(**d_layer["LSTM"], input_shape=(n_inputs, 1)))
            # Is this the last output layer? If so, set units to prediction days
            elif idx_layer == (len(Recurrent_Neural_Network) - 1):
                model.add(LSTM(**d_layer["LSTM"], units=n_days))
            else:
                model.add(LSTM(**d_layer["LSTM"]))

        # Dense (Simple Neuron)
        elif str(*d_layer) == "Dense":
            # Is this the input layer? If so, define input_shape
            if idx_layer == 0:
                model.add(Dense(**d_layer["Dense"], input_dim=n_inputs))
            # Is this the last output layer? If so, set units to prediction days
            elif idx_layer == (len(Recurrent_Neural_Network) - 1):
                model.add(Dense(**d_layer["Dense"], units=n_days))
            else:
                model.add(Dense(**d_layer["Dense"]))

        # Conv1D Layer
        elif str(*d_layer) == "Conv1D":
            if idx_layer == 0:
                model.add(
                    Conv1D(**d_layer["Conv1D"], input_shape=(n_inputs, 1)))
            else:
                model.add(Conv1D(**d_layer["Conv1D"]))
        # Max Pooling Layer for after Conv Layer
        elif str(*d_layer) == "MaxPool1D":
            model.add(MaxPool1D(**d_layer["MaxPool1D"]))
        # Allow for if user wants to do average pooling
        elif str(*d_layer) == "AvgPool1D":
            model.add(AvgPool1D(**d_layer["AvgPool1D"]))
        # Dropout (Regularization)
        elif str(*d_layer) == "Dropout":
            model.add(Dropout(**d_layer["Dropout"]))
        # Flatten layer for Convolutions
        elif str(*d_layer) == "Flatten":
            model.add(Flatten())
        else:
            print(f"Incorrect neuron type: {str(*d_layer)}")

    return model
Beispiel #4
0
    def build(self, hp):
        ###### Setup hyperparamaters
        pooling_1 = hp.Choice('pooling_1', ['avg', 'max'])
        pooling_2 = hp.Choice('pooling_2', ['avg', 'max'])
        pooling_3 = hp.Choice('pooling_3', ['avg', 'max'])
        dense_units = hp.Int('dense_units',
                             min_value=16,
                             max_value=128,
                             step=16)
        dropout = hp.Float('dropout', 0.1, 0.6)
        bias_constant = hp.Float('bias', 0.01, 0.03)
        optimizer = hp.Choice('optimizer', values=['adam', 'sgd', 'rmsprop'])

        ###### Construct model
        # Instantiate sequential model.
        model = Sequential()
        model.add(
            Conv1D(self.filters,
                   self.kernel_size,
                   padding='same',
                   activation='relu',
                   input_shape=self.input_shape))
        if pooling_1 == 'max':
            model.add(MaxPool1D())
        else:
            model.add(AvgPool1D())
        model.add(
            Conv1D(self.filters * 2,
                   self.kernel_size,
                   padding='same',
                   activation='relu'))
        if pooling_2 == 'max':
            model.add(MaxPool1D())
        else:
            model.add(AvgPool1D())
        model.add(
            Conv1D(self.filters * 4,
                   self.kernel_size,
                   padding='same',
                   activation='relu'))
        if pooling_3 == 'max':
            model.add(MaxPool1D())
        else:
            model.add(AvgPool1D())
        # Add dropout layer before flattening
        model.add(Dropout(dropout))
        # Flatten for use with fully-connected layers
        model.add(Flatten())
        model.add(Dense(dense_units, activation='relu'))
        # Regularization layer using dropout
        model.add(Dropout(dropout))
        # Output layer with Softmax activation due to multivariate value
        model.add(
            Dense(self.features,
                  kernel_initializer=he_uniform(seed=0),
                  bias_initializer=Constant(bias_constant),
                  activation='sigmoid'))
        # Compile model
        model.compile(
            optimizer=self.get_optimizer(hp, optimizer),
            loss='mse',
        )
        return model
dx = np.asarray(array_text)
print(dx.shape)

dy = np.asarray(binary)
from sklearn.model_selection import train_test_split as tts
xtrain, xtest, ytrain, ytest = tts(dx,
                                   dy,
                                   test_size=.2,
                                   random_state=101,
                                   stratify=dy)

model = Sequential()
model.add(Conv1D(max_length, 3, input_shape=(max_length, size)))
model.add(Activation("sigmoid"))
model.add(AvgPool1D(pool_size=3))
#model.add(Dropout(0.2))

model.add(Conv1D(max_length, 3))
model.add(Activation("sigmoid"))
model.add(MaxPool1D(pool_size=3))
model.add(Dropout(0.2))

model.add(Conv1D(max_length, 2))
model.add(Activation("sigmoid"))
model.add(AvgPool1D(pool_size=2))
model.add(Dropout(0.2))

model.add(Conv1D(max_length, 2))
model.add(Activation("sigmoid"))
model.add(MaxPool1D(pool_size=2))