Reputation: 365
I do have 2 inputs X, and Y, and the output C is in one-hot encoding [0,1] or [1,0].
The size of X,Y (transposed) matrix in 2 * 320 and the size of C is 1 * 320.
how shall I write the network in Keras in such a way that :
<> layer 0 : has 2 inputs (and 2 nodes)
<> layer 1 has 4 units and a linear activation function from layer 0 to layer 1
<> layer 2 has 1 node (and a softmax activation function ? from layer 1 to layer 2).
I would need to perform the binary classification by using this network. Thanks !
Upvotes: 0
Views: 66
Reputation: 1117
wrote with the functional API.
inputs = keras.Input(shape=(2,)) # layer 0
layer = layers.Dense(4, activation="linear")(inputs) # layer 1
outputs = layers.Dense(1, activation="softmax")(layer ) # layer 2
model = keras.Model(inputs=inputs, outputs=outputs, name="noname")
UPDATE cause im in for a fight:
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam
def gen_model():
inputs = keras.Input(shape=(2,)) # layer 0
layer = layers.Dense(4, activation="linear")(inputs) # layer 1
outputs = layers.Dense(1, activation="softmax")(layer ) # layer 2
model = keras.Model(inputs=inputs, outputs=outputs, name="noname")
model.compile(loss=tf.keras.losses.MeanSquaredError(name="mse"), optimizer=Adam(), metrics=None) # Adam(learning_rate=1e-2)
return model
X = df[['X','Y']].values # assuming the data are in a pandas dataframe
y = df['C'].values
model = gen_model()
model.fit(x=X, y=y, batch_size=32, epochs=500, validation_split=0.2)
Upvotes: 1
Reputation: 547
Here is a sample code:
input_layer = Input(shape=(np.transpose(X).shape[1],))
dense_layer_1 = Dense(4, activation='relu')(input_layer)
output = Dense(np.transpose(C).shape[1], activation='softmax')(dense_layer_1)
model = Model(inputs=input_layer, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
Upvotes: 1