generative network with dense layers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Model | |
from keras.layers import Input, Dense | |
from keras.optimizers import Adam | |
# Define the input layer | |
inputs = Input(shape=(784,)) | |
# Define the hidden layers | |
hidden1 = Dense(128, activation='relu')(inputs) | |
hidden2 = Dense(64, activation='relu')(hidden1) | |
# Define the output layer | |
outputs = Dense(10, activation='softmax')(hidden2) | |
# Define the model | |
model = Model(inputs=inputs, outputs=outputs) | |
# Compile the model | |
optimizer = Adam(lr=0.001) | |
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) | |
# Train the model | |
model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from keras.layers import Layer, Input | |
class AttentionLayer(Layer): | |
def __init__(self, units): | |
super(AttentionLayer, self).__init__() | |
self.W1 = tf.keras.layers.Dense(units) | |
self.W2 = tf.keras.layers.Dense(units) | |
self.V = tf.keras.layers.Dense(1) | |
def call(self, features, hidden_state): | |
hidden_with_time_axis = tf.expand_dims(hidden_state, 1) | |
score = tf.nn.tanh(self.W1(features) + self.W2(hidden_with_time_axis)) | |
attention_weights = tf.nn.softmax(self.V(score), axis=1) | |
context_vector = attention_weights * features | |
context_vector = tf.reduce_sum(context_vector, axis=1) | |
return context_vector, attention_weights | |
class MyModel(tf.keras.Model): | |
def __init__(self, vocab_size, embedding_dim, units, sequence_length, batch_size): | |
super(MyModel, self).__init__() | |
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim) | |
self.gru = tf.keras.layers.GRU(units, return_sequences=True, return_state=True) | |
self.attention = AttentionLayer(units) | |
self.fc = tf.keras.layers.Dense(vocab_size) | |
# Store sequence_length and batch_size as attributes | |
self.sequence_length = sequence_length | |
self.batch_size = batch_size | |
def call(self, inputs, hidden_state): | |
embedded = self.embedding(inputs) | |
outputs, state = self.gru(embedded, initial_state=hidden_state) | |
context_vector, attention_weights = self.attention(outputs, state) | |
out = self.fc(context_vector) | |
return out, state, attention_weights | |
from keras.models import Model | |
from keras.layers import Input, Dense, GRU | |
from keras.optimizers import Adam | |
import numpy as np | |
# Define the input layer | |
inputs = Input(shape=(784,)) | |
# Define the hidden layers | |
hidden1 = Dense(128, activation='relu')(inputs) | |
hidden2 = Dense(64, activation='relu')(hidden1) | |
outputs, state = tf.keras.layers.GRU(4, return_sequences=True, return_state=True)(hidden2[:, np.newaxis, :]) | |
context_vector, attention_weights = AttentionLayer(64)(outputs, state) | |
# Define the output layer | |
outputs = Dense(10, activation='softmax')(context_vector) | |
# Define the model | |
model = Model(inputs=inputs, outputs=outputs) | |
# Compile the model | |
optimizer = Adam(lr=0.001) | |
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) | |
model.summary() | |
# Train the model | |
#model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from keras.models import Sequential | |
from keras.layers import LSTM, Dense, Dropout, BatchNormalization | |
from keras.optimizers import Adam | |
# Define the number of time steps in each sequence | |
timesteps = 10 | |
# Define the size of the latent space | |
latent_dim = 10 | |
# Define the number of features in each time step | |
num_features = 10 | |
# Define the generator model | |
generator = Sequential() | |
#generator.add(LSTM(64, input_shape=(timesteps, latent_dim))) | |
#generator.add(BatchNormalization()) | |
#generator.add(Dropout(0.3)) | |
generator.add(Dense(10, activation='relu', input_shape=(10,)))#.add(Dense(num_features, activation='sigmoid')) | |
generator.add(Dropout(0.3)) | |
generator.add(Dense(10, activation='relu', input_shape=(10,)))#.add(Dense(num_features, activation='sigmoid')) | |
# Define the discriminator model | |
discriminator = Sequential() | |
discriminator.add(Dense(10, activation='relu', input_shape=(10,))) | |
# Define the combined model (GAN) | |
discriminator.trainable = False | |
gan = Sequential() | |
gan.add(generator) | |
gan.add(discriminator) | |
# Compile the models | |
optimizer = Adam(lr=0.0002, beta_1=0.5) | |
discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) | |
gan.compile(loss='binary_crossentropy', optimizer=optimizer) | |
# Define a static dummy data for training | |
dummy_data = np.random.rand(50, timesteps) | |
# Train the GAN | |
batch_size = 16 | |
epochs = 100000 | |
for epoch in range(epochs): | |
# Train the discriminator | |
real_samples = dummy_data[np.random.choice(dummy_data.shape[0], batch_size, replace=False)] | |
fake_samples = generator.predict(np.random.normal(0, 1, size=(batch_size, timesteps))) | |
# print(fake_samples , real_samples) | |
discriminator_loss_fake = discriminator.train_on_batch(fake_samples, np.zeros((batch_size,10))) | |
discriminator_loss_real = discriminator.train_on_batch(real_samples, np.ones((batch_size,10))) | |
# Train the generator | |
noise = np.random.normal(0, 1, size=(batch_size, timesteps)) | |
generator_loss = gan.train_on_batch(noise, np.ones((batch_size,10))) | |
# Print the losses | |
print('Epoch: %d, Generator Loss: %f' % (epoch+1, generator_loss)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment