0

I am using Keras to do a prediction. I train it using two arrays of numbers, Y1 and Y2 for each input number X.

I want to get a prediction of a _Y1 and a _Y2, but I don't know how.

The visualization shows nothing. That makes sense. Checking it, it looks like the "predicition" array is empty.

import numpy as np
import pandas
import math
import random
from keras.models import Sequential, Model
from keras.layers import Input, Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
import graphviz
import pydot
import matplotlib.pyplot as plt 

X = np.array([])
Y_int = np.array([])
Y1 = np.array([])
Y2 = np.array([])


count = -1

while count < 1:
    count += 0.001
    X += np.array([count])
    i = ( math.sin(count) )
    Y_int += np.array([i])
    if i > 1 or i < -1:
        o = 1
        u = 1 / i
    else:
        o = i
        u = 1
    Y1 += np.array([o])
    Y2 += np.array([u])

length = len(X)

# define base model
def baseline_model():
    # create model
    model = Sequential()
    inp = Input((1,))
    x = Dense(100, kernel_initializer='lecun_normal', activation='tanh')(inp)
    out1 = Dense(1, kernel_initializer='lecun_normal')(x)
    out2 = Dense(1, kernel_initializer='lecun_normal')(x)
    model = Model(inp, [out1,out2])
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model

estimator = baseline_model();
estimator.fit(X, [Y1, Y2], batch_size = 20, epochs = 1, verbose = 0)

plt.axis([-1, 1, -1, 1])
plt.ion()

while True:
    estimator.fit(X, [Y1, Y2], batch_size = 20, epochs = 1, verbose = 0)
    prediction = estimator.predict(X)
    plt.clf()
    plt.plot(X, Y_int)
    plt.plot(X, prediction )
    plt.pause(0.001)

1 Answer 1

1

+= is not the correct way for appending numpy arrays (I assume that this is your purpose); here is the result of your first while loop:

count = -1

while count < 1:
    count += 0.001
    X += np.array([count])
    i = ( math.sin(count) )
    Y_int += np.array([i])
    if i > 1 or i < -1:
        o = 1
        u = 1 / i
    else:
        o = i
        u = 1
    Y1 += np.array([o])
    Y2 += np.array([u])

length = len(X)

length
# 0
X
# array([], dtype=float64)
Y1
# array([], dtype=float64)
Y2
# array([], dtype=float64)

i.e. all your arrays are still empty...

If appending the arrays is indeed your purpose, use the numpy.append method:

count = -1

while count < 1:
    count += 0.001
    X = np.append(X, np.array([count]))
    i = math.sin(count))
    Y_int = np.append(Y_int, np.array([i]))
    if i > 1 or i < -1:
        o = 1
        u = 1 / i
    else:
        o = i
        u = 1
    Y1 = np.append(Y1, np.array([o]))
    Y2 = np.append(Y2, np.array([u]))

length = len(X)

length
# 2000
len(Y1), len(Y2), len(Y_int)
# (2000, 2000, 2000)

Moreover, you should remove the model = Sequential() line from your model definition, since you are apparently using the Keras Functional API.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.