Do any of you know why I get the following error code?
My Code :
JavaScript
x
47
47
1
import numpy as np
2
import tensorflow as tf
3
from tensorflow import keras
4
import matplotlib.pyplot as pl
5
from casadi import *
6
x = SX.sym("x",2)
7
f = vertcat(x[0]-x[0]*x[1],
8
-x[1]+x[0]*x[1])
9
dae = dict(x = x,ode = f)
10
11
# Create integrator
12
def Simulation(xst):
13
t=0
14
X1=list()
15
X2=list()
16
T=list()
17
while t<=10 :
18
op = dict(t0=0, tf=t)
19
F = integrator("F", "cvodes", dae, op)
20
r = F(x0 = [xst[0],xst[1]])
21
X1.append(float(r["xf"][0]))
22
X2.append(float(r["xf"][1]))
23
T.append(t)
24
t=t+1
25
return(X1,T)
26
Simulation([1,2])
27
28
model=tf.keras.Sequential([
29
keras.layers.Dense(1,input_shape=[2]),
30
])
31
32
model.compile(optimizer="sgd" , loss="mean_squared_error")
33
34
input= np.array([[1,2],[2,3],[4,1],[5,3],[1,3],[3,1],[6,4],[5,2],[1,5],[8,3]])
35
36
37
def output():
38
Out=[[]]
39
for i in range(0,len(input)):
40
X1,T=Simulation(input[i])
41
maxA=max(X1)
42
Out=np.append(Out,[maxA])
43
return (Out)
44
model.fit(input,output(),epochs=10)
45
test=np.array([2,1])
46
print(model.predict(test))
47
You can ignore the Integrator Part, I just want to know why the model.predict wont work. Here is the error:
JavaScript
1
31
31
1
Traceback (most recent call last):
2
File "C:/Users/User/PycharmProjects/pythonProject3/main.py", line 47, in <module>
3
print(model.predict(test))
4
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
5
raise e.with_traceback(filtered_tb) from None
6
File "C:UsersUserAppDataLocalTemp__autograph_generated_filedjega_6c.py", line 15, in tf__predict_function
7
retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
8
ValueError: in user code:
9
10
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasenginetraining.py", line 1845, in predict_function *
11
return step_function(self, iterator)
12
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasenginetraining.py", line 1834, in step_function **
13
outputs = model.distribute_strategy.run(run_step, args=(data,))
14
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasenginetraining.py", line 1823, in run_step **
15
outputs = model.predict_step(data)
16
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasenginetraining.py", line 1791, in predict_step
17
return self(x, training=False)
18
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasutilstraceback_utils.py", line 67, in error_handler
19
raise e.with_traceback(filtered_tb) from None
20
File "C:UsersUserPycharmProjectspythonProject1venvlibsite-packageskerasengineinput_spec.py", line 228, in assert_input_compatibility
21
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
22
23
ValueError: Exception encountered when calling layer "sequential" (type Sequential).
24
25
Input 0 of layer "dense" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (None,)
26
27
Call arguments received by layer "sequential" (type Sequential):
28
• inputs=tf.Tensor(shape=(None,), dtype=int32)
29
• training=False
30
• mask=None
31
Advertisement
Answer
The problem is with the lines:
JavaScript
1
3
1
test=np.array([2,1])
2
print(model.predict(test))
3
Here your model is setup to receive a rank 2 tensor as input, but you are only giving it a rank 1 tensor (vector) as input. You need to expand the dimension by 1, like this:
JavaScript
1
3
1
test=np.array([[2,1]])
2
print(model.predict(test))
3
You will then be giving it a test.shape = (1,2)
tensor (rank 2) and it should now work without error.