'How to assign neural network output as either two outputs[0,1]?
I have coded an MLP that has 30 input, 15 hidden and 2 output neurons. I'm trying to work out how to encode my output to be [1,0], using two classes to encode it ( m = [0,1] b = [1,0])
the section of my code below is me assigning my y and x values from my dataset, where I then initialize the layers, and begin generating my weights. Do I need to input some sort of encoding in this section? When I run my whole program I get an error stating the data and indices are not aligned (my output layer = 2 is causing this issue).
This is because im trying to make the output layer 2 neurons as opposed to 1, when I run with the value 1, my program works fine, but only has the 1 layer for the output and I need 2.
X = trainingdata.iloc[:,1:].to_numpy()
Y = trainingdata.iloc[:,0].to_numpy().reshape(-1, 1)
print(Y)
print(X)
#assigning variables for iterations later on, aswell as number of hidden neurons
l_rate = 0.05
epochs = 10
Hidden_Neurons = 15
bias = 0.5
output_neurons = 2
print(bias)
print("first bias")
#initialising weights for use in model - initialised at random, taking in number of hidden neurons for randomisation.
np.random.seed(seed=1)
h_weights = np.random.rand(X.shape[1], Hidden_Neurons)
O_weights = np.random.rand(Hidden_Neurons, output_neurons)
below is where my calculations happen and the data is outputted
# activation function for my hidden layer
O_prediction = linear(X, h_weights, bias)
O_prediction = sigmoid(O_prediction)
# activation for my output layer
O_prediction = linear(O_prediction, O_weights, bias)
O_prediction = sigmoid(O_prediction)
#Update and pass back new updated bias value
def update_bias(bias, l_rate, Y, O_prediction):
return bias + l_rate*(Y-O_prediction)
#print and view new bias values
new_bias = update_bias(bias, l_rate, Y, O_prediction)
print("New bias:")
print(new_bias)
# Transfer output value to this format
O_prediction = pd.DataFrame(
O_prediction,
columns=["prediction"],
index=trainingdata.index)
#concrecating all data together with original dataframe
output = pd.concat(
(trainingdata, O_prediction),
axis=1)
output['Loss'] = output['Diagnosis'] - output['prediction']
print("Prediction vs values returned:")
print(output)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
