Skye
Skye

Reputation: 300

Create a square function estimator with Keras

I'm still very new to neural networks. I try to achieve the following with Keras:

I have a set of data where f(x) = x^2 + 3. Like this:

x       f(x)
 -10    103
-9.9    101.01
-9.8    99.04
-9.7    97.09
...
9.7     97.09
9.8     99.04
9.9    101.01
10     103

So I try to build a model that can predict values f(x) based on x. I think that must be a simple thing but I couldn't find any hint. I get only outputs ranging from 0 to 1 (I guess due to normalization?) and they also seem to be crap.

# Import the dataset
dataset = pd.read_csv("simple_network/Linear Data.csv", header=None).values
X_train, X_test, Y_train, Y_test = train_test_split(dataset[:,0:1], dataset[:,1], 
                                                    test_size=0.25,)
# Now we build the model
neural_network = Sequential() # create model
neural_network.add(Dense(5, input_dim=1, activation='sigmoid')) # hidden layer
neural_network.add(Dense(1, activation='sigmoid')) # output layer
neural_network.compile(loss='mean_squared_error', optimizer='sgd', metrics=['accuracy'])
neural_network_fitted = neural_network.fit(X_train, Y_train, epochs=1000, verbose=0, 
                                           batch_size=X_train.shape[0], initial_epoch=0)

I suspect I need to somehow cater for the fact that expect an interval value as an output, not a nominal or ordinal value. Any idea?

Upvotes: 2

Views: 809

Answers (1)

Chan Kha Vu
Chan Kha Vu

Reputation: 10400

First, you use sigmoid as the activation function for your output layer. If you look at the formula of the sigmoid function (see wiki), you can see that its output values lies in the range from 0.0 to 1.0. So, try to replace the activation function of the last layer to linear. This way, the non-linearity will be handled by your hidden layer (with a non-linear activation function), and the output layer will be used for scaling and adding bias if needed.

Upvotes: 1

Related Questions