Neural network sine approximation

后端 未结 3 1258
粉色の甜心
粉色の甜心 2020-12-07 05:07

After spending days failing to use neural network for Q learning, I decided to go back to the basics and do a simple function approximation to see if everything was working

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 05:52

    A more concise version of your code that works:

    def data_gen():
        while True:
            x = (np.random.random([1024])-0.5) * 10 
            y = np.sin(x)
            yield (x,y)
    
    regressor = Sequential()
    regressor.add(Dense(units=20, activation='tanh', input_dim=1))
    regressor.add(Dense(units=20, activation='tanh'))
    regressor.add(Dense(units=20, activation='tanh'))
    regressor.add(Dense(units=1, activation='linear'))
    regressor.compile(loss='mse', optimizer='adam')
    
    regressor.fit_generator(data_gen(), epochs=3, steps_per_epoch=128)
    
    x = (np.random.random([1024])-0.5)*10
    x = np.sort(x)
    y = np.sin(x)
    
    plt.plot(x, y)
    plt.plot(x, regressor.predict(x))
    plt.show()
    

    Changes made: replacing low layer activations with hyperbolic tangents, replacing the static dataset with a random generator, replacing sgd with adam. That said, there still are problems with other parts of your code that I haven't been able to locate yet (most likely your scaler and random process).

提交回复
热议问题