How to build a Neural Network to multiply two numbers

时光总嘲笑我的痴心妄想 提交于 2021-01-29 05:12:33

问题


I am trying to build a neural network which would multiply 2 numbers. To do the same, I took help of scikit-learn. I am going for a neural network with 2 hidden layers, (5, 3) and ReLU as my activation function.

I have defined my MLPRegressor as follows:

X = data.drop('Product', axis=1)
y = data['Product']
X_train, X_test, y_train, y_test = train_test_split(X, y)
scaler = StandardScaler()
scaler.fit(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
mlp = MLPRegressor(hidden_layer_sizes=(5, 3), activation="relu", learning_rate="adaptive", max_iter=500000, verbose=True, validation_fraction=0.25)

Here, data is the dataframe which contains the 3 columns, 2 random numbers and 1 Product column. The issue is the loss that I get is of order 10^14. How do I reduce this loss, improve my model performance and what all possible changes can help me out in this situation?


回答1:


I believe that an NN with only two hidden layers is not sufficient to perform a multiplication of arbitrary numbers. Note that a multiplication of N * M is equal to the M-fold addition of N. There are neural networks that can perform an addition, but you have to consider the general conditions. Furthermore, you have to decide how the inputs and outputs of your network are defined: Do you want two input neurons and one output neuron, or do you want the two multiplicants to be binary (as a vector) in the net? I think you would have to normalize your input values if you gave decimal values in two input neurons.




回答2:


I am not an expert in NNs. I would do log-tranform of the inputs, then feed them into a network, then exponetiate the output. Just a thought.




回答3:


There is a way to do this. This is a neural network that approximate the multiplication function to multiply numbers in the range of 1000 - 10000, and it could work really well if the range of numbers is limited. Here is the gist link



来源:https://stackoverflow.com/questions/52660944/how-to-build-a-neural-network-to-multiply-two-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!