Python Forum

Full Version: Error with output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following code, throwing the error: can some help out with the executable code for the same
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Flatten
from sklearn.model_selection import train_test_split

# Function to generate synthetic BZ reaction data
def generate_bz_data(num_samples, spatial_dim, temporal_dim):
    data = np.random.rand(num_samples, spatial_dim, temporal_dim, 1)
    labels = np.random.randint(0, 2, size=(num_samples, spatial_dim, temporal_dim, 1))
    return data, labels

# Generate synthetic BZ reaction data
num_samples = 1000
spatial_dim = 32
temporal_dim = 20
X, y = generate_bz_data(num_samples, spatial_dim, temporal_dim)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Build an RNN model
model = Sequential()
model.add(LSTM(50, input_shape=(spatial_dim, temporal_dim, 1), return_sequences=True))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))

# Compile the model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Train the model
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))

# Generate new spatiotemporal patterns using the trained model
new_data = np.random.rand(5, spatial_dim, temporal_dim, 1)
generated_patterns = model.predict(new_data)

# Visualize the generated patterns
fig, axes = plt.subplots(1, 5, figsize=(15, 3))
for i in range(5):
    axes[i].imshow(np.squeeze(new_data[i, :, :, :]), cmap='viridis')
    axes[i].set_title(f'Generated Pattern {i+1}')
    axes[i].axis('off')

plt.show()
What is the error?