'ValueError: Input 0 of layer hiddenL1 is incompatible with the layer: its rank is undefined, but the layer requires a define rank
I am trying to create a sequential keras model with custom weights. The weights come from a row in a numpy array. When running the code I get the error:
ValueError: Input 0 of layer hiddenL1 is incompatible with the layer: its rank is undefined, but the layer requires a defined rank.
I am not sure what other information will be useful here, feel free to comment if you would like more information.
df = pd.read_csv('NASDAQ.csv')
array = df.to_numpy()
array = np.delete(array, 0,1)
inc = []
maxs = np.max(array,axis=0)
for i in range(len(array)):
for j in range(len(array[0])):
array[i][j] = array[i][j]/maxs[j]
population_size = 100
output11 = 1
activation = 'elu'
out_activation='sigmoid'
epochs = 500
opt = SGD(lr=.01, momentum =.9)
weights = [[]for _ in range(population_size)]
for i in range(population_size):
for j in range(6):
weights[i].append(random.uniform(0, 1000))
for i in range(len(array)):
if i == 0:
inc.append(0)
elif array[i][4] > array[i-1][4]:
inc.append(1)
elif array[i][4] <= array[i-1][4]:
inc.append(0)
inc = np.array(inc)
inc = np.reshape(inc, (-1,1))
array = np.hstack((array, inc))
input_train = array[0:1259,0:6]
output_train = array[0:1259, 6]
input_validate = array[1259:1887, 0:6]
output_validate = array[1259:1887, 6]
input_test = array[1887: , 0:6]
output_test = array[1887: , 6]
weights1 = np.array(weights[0])
weights1.shape = (6,1)
out_test2d =np.reshape(output_test,(len(output_test),1))
out_train2d = np.reshape(output_test, (len(output_test),1))
weights1 = np.array(weights[0])
weights1.shape=(6,)
model = Sequential();
model.add(InputLayer())
model.add(Dense(6, use_bias = True, name = "hiddenL1", input_shape = (None,6)) )
model.add(Dense(output11, use_bias=True, name = "output", activation = out_activation))
opt = tf.keras.optimizers.Adam(learning_rate=0.01, epsilon=None, decay=0.05, beta_1 = 1,beta_2 = .5,amsgrad=False)
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.layers[0].set_weights([weights1])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|