remigenet / TKAT

Temporal Kolmogorov-Arnold Transformer
Other
67 stars 12 forks source link

ValueError: Layer functional_7 weight shape (1, 100, 10) is not compatible with provided weight shape (1, 10). #10

Open Thaumaturge2020 opened 2 weeks ago

Thaumaturge2020 commented 2 weeks ago
import numpy as np
import tensorflow as tf

N_MAX_EPOCHS = 100
BATCH_SIZE = 128
early_stopping_callback = lambda : tf.keras.callbacks.EarlyStopping(
    monitor="val_loss",
    min_delta=0.00001,
    patience=6,
    mode="min",
    restore_best_weights=True,
    start_from_epoch=6,
)
lr_callback = lambda : tf.keras.callbacks.ReduceLROnPlateau(
    monitor="val_loss",
    factor=0.25,
    patience=3,
    mode="min",
    min_delta=0.00001,
    min_lr=0.000025,
    verbose=0,
)
callbacks = lambda : [early_stopping_callback(), lr_callback(), tf.keras.callbacks.TerminateOnNaN()]

sequence_length = 30
num_unknow_features = 8
num_know_features = 2
num_embedding = 1
num_hidden = 100
num_heads = 4
num_samples = 3
n_ahead = 10
use_tkan = True

model = TKAT(sequence_length, num_unknow_features, num_know_features, num_embedding, num_hidden, num_heads, n_ahead, use_tkan = use_tkan)
optimizer = tf.keras.optimizers.Adam(0.001)
model.compile(optimizer=optimizer, loss='mean_squared_error')

model.summary()

# X_train should be a numpy array of shape (n_samples, sequence_length + n_ahead, num_unknow_features + num_know_features)
# and y_train should be a numpy array of shape (n_samples, n_ahead).
# The values in X_train[:,sequence_length:,:num_unknow_features] are not used and can be set to 0.
# The known inputs should be the last features in X_train.

X_train = np.zeros((num_samples,sequence_length+n_ahead,num_unknow_features+num_know_features))

y_train = np.zeros((num_samples,n_ahead))

X_test = np.zeros((sequence_length+n_ahead,num_unknow_features+num_know_features))

y_test = np.zeros((n_ahead))

for i in range(num_samples):
    normal_array = np.arange(i*3+1,i*3+sequence_length+n_ahead+1)
    X_train[i,:sequence_length,num_unknow_features] = np.sin(normal_array[:sequence_length])
    X_train[i,:sequence_length,num_unknow_features+1] = np.cos(normal_array[:sequence_length])
    y_train[i,:] = normal_array[sequence_length:]

normal_array = np.arange(num_samples*3+1,num_samples*3+sequence_length+n_ahead+1)
X_test[:sequence_length,num_unknow_features] = np.sin(normal_array[:sequence_length])
X_test[:sequence_length,num_unknow_features+1] = np.cos(normal_array[:sequence_length])
y_test[:] = normal_array[sequence_length:]

print("??")
history = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=N_MAX_EPOCHS, validation_split=0.2, callbacks=callbacks(), shuffle=True, verbose = False)

print("??")
test_preds = model.predict(X_test)

print(y_test)
print("??")
print(test_preds)

I add a simple test in jupyter as above.

but it simple can't get work...

image like it,lol

remigenet commented 2 weeks ago

Could you give your package version - I just created a vast instance on which I freshly installed tkat and tensorflow to give a test and it works. There were just a mistake when you did "model.predict(X_test)", as X_test shape is (40 ,3) while the model except (num_sample, 40, 3), so I just had to change by model.predict(X_test[np.newaxis,...]) to add a first dimension to it.

image

If you have older package version there were an issue with a version in the KANLinear layer used and imported but resolved since, so if you can give your package version for tkat, tkan and keras_efficient_kan it could help found out

Thaumaturge2020 commented 2 weeks ago

Could you give your package version - I just created a vast instance on which I freshly installed tkat and tensorflow to give a test and it works. There were just a mistake when you did "model.predict(X_test)", as X_test shape is (40 ,3) while the model except (num_sample, 40, 3), so I just had to change by model.predict(X_test[np.newaxis,...]) to add a first dimension to it.

image

If you have older package version there were an issue with a version in the KANLinear layer used and imported but resolved since, so if you can give your package version for tkat, tkan and keras_efficient_kan it could help found out

I used 0.4.2 tkan

remigenet commented 2 weeks ago

That's surely why, I forgot to update the tkat pyproject and package to update the requirements when fixing this in the TKAN package in version 0.4.3 I just updated the TKAT package to now requires at least tkan 0.4.3 Could you please do pip install tkat --upgrade to have new version (0.2.2) and check if the problem is solved ?