Categories
Misc

I was making a GAN to write poem , but got this error

the code:

from google.colab import drive
drive.mount(‘/content/drive’)
from google.colab import files
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
import numpy as np
import string
from tensorflow.keras.utils import to_categorical
import os
from tensorflow.keras import regularizers
import pickle
import sys
from tensorflow.keras.preprocessing.sequence import pad_sequences
import tensorflow as tf
from tensorflow.keras.layers import Dense, SimpleRNN, Embedding,GRU,Flatten,LSTM,Conv2D,MaxPool2D,Dropout,ConvLSTM2D,Reshape
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import to_categorical
def clean_text(text):
words = text.split()
table = str.maketrans(”,”, string.punctuation)
words = [w.translate(table) for w in words]
words = [word for word in words if word.isalpha()]
return words
def generate_sequences(words):
length = 10 + 1
sentences = []
for i in range(length, len(words)):
seq = words[i-length:i]
line = ‘ ‘.join(seq)
sentences.append(line)
return sentences
def process_data(path):
raw_data = open(path, ‘r’, encoding=’utf-8′).read()
lower_text = raw_data.lower()
words = clean_text(lower_text)
sentences = generate_sequences(words)
return sentences
def create_training_data(tokenizer, numeric_sentences):
vocabulary_size = len(tokenizer.word_index) + 1
data_array = np.array(numeric_sentences)
X, y = data_array[:,:-1], data_array[:,-1]
y = to_categorical(y, num_classes=vocabulary_size)
input_length = X.shape[1]
return X, y, vocabulary_size,input_length
def build_rnn_model(vocabulary_size,input_length):

model = Sequential()
model.add(Embedding(vocabulary_size, 50, input_length = input_length))
model.add(Dense(50,activation=’relu’))
model.add(Dense(75,activation=’relu’))
model.add(LSTM(25, return_sequences=True))
model.add(SimpleRNN(25))
model.add(Dense(50,activation=’relu’))
model.add(Dense(vocabulary_size, activation=’softmax’))
return model
def train(model, batch_size, epochs, learning_rate,X,y):
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer = optimizer, metrics=[‘accuracy’])
with tf.GradientTape() as tape:
pred=model(X)
loss = tf.keras.losses.CategoricalCrossentropy()(y, pred)
grads = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
history = model.fit(X,y,batch_size=batch_size,epochs=epochs)
return history, model
from pickle import dump
path = ‘data.txt’
sentences = process_data(path)
from tensorflow.keras.preprocessing.text import Tokenizer
def tokenize_sentence(sentences):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(sentences)
numeric_sentences = tokenizer.texts_to_sequences(sentences)
return tokenizer, numeric_sentences
tokenizer, numeric_sentences = tokenize_sentence(sentences)
X,y,vocabulary_size,input_length = create_training_data(tokenizer, numeric_sentences)
model = build_rnn_model(vocabulary_size,input_length)
print(“Starting the traing of the model: “)
batch_size = 128
epochs = 200
learning_rate = 0.0009
#train(model, batch_size, epochs, learning_rate, X,y)
#dump(tokenizer, open(‘C:/Users/HP/Desktop/poem writer/tokenizer.pkl’, ‘wb’))
def poem_generator():
user_input = input(“Write the first line of your poem, the poem generator will complete it!! n>> “)
in_text = user_input.lower()
sys.stdout.write(‘nnYour Poemnn’)
start = ‘ ‘+ in_text+’n’
sys.stdout.write(start)
for i in range(110):
encoded = tokenizer.texts_to_sequences([in_text])[0]
encoded = pad_sequences([encoded], maxlen = input_length, truncating = ‘pre’)
yhat = model.predict(encoded, verbose = 0)
yhat=np.argmax(yhat,axis=1)
out_word = ”
for word, index in tokenizer.word_index.items():
if index == yhat:
out_word = word
break
in_text += ‘ ‘ + out_word
out_word = ‘ ‘ + out_word
if i % 7 ==0 and i !=0:
out_word = out_word + ‘n’
sys.stdout.write(out_word)
sys.stdout.flush()
sys.stdout.write(‘nn’)

with open(‘tokenizer.pkl’ , ‘rb’) as f:
tokenizer = pickle.load(f)
vocabulary_size = len(tokenizer.word_index) + 1
input_length = 10
print(model.summary())
#poem_generator()
def disciminator(inp):
model=Sequential()
model.add(Dense(1285,input_shape=inp))
#model.add(Conv2D(50,1,input_shape=input_shape))
model.add(Dense(75,activation=’relu’))
#model.add(Conv2D(50,1))
model.add(Dense(50,activation=’softmax’))
model.add(Dropout(0.3))
model.add(Dense(10,activation=’sigmoid’))
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0009)
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer = optimizer, metrics=[‘accuracy’])
return model
def generator():
model = Sequential()
model.add(Embedding(vocabulary_size, 50, input_shape=(vocabulary_size,)))
model.add(Dense(50,activation=’relu’))
model.add(Dense(10,activation=’relu’))
#model.add(LSTM(25,return_sequences=True))
model.add(Dense(50,activation=’relu’))
model.add(Dense(10, activation=’softmax’))
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0009)
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer = optimizer, metrics=[‘accuracy’])
return model
def gan(gen,dis):
dis.trainable = False
model = Sequential()
model.add(gen)
model.add(dis)
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0009)
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer = optimizer, metrics=[‘accuracy’])
return model
def generate_real_samp(n_samp):
X1=np.random.randint(0,input_length,vocabulary_size)
x=X[X1]
y=np.ones((vocabulary_size,input_length))
return x.reshape(1285,10),y.reshape(1285,10)
def generate_patien_poi(lat,n_samp):
x_inp=np.random.randn((n_samp**2)*(lat))
x_inp=x_inp.reshape(1285,1285,10)
return x_inp
def cre_fake_po(generator,lat,n_samp):
x_input = generate_patien_poi( n_samp,lat)
x = generator.predict(x_input)
y = np.zeros((3274,10))
return X, y
def trainer(g_model, d_model, gan_model, lat, n_epochs=200, n_batch=128):
h_b=int(n_batch/2)
for i in range(epochs):
x_real,y_real=generate_real_samp(h_b)
print(x_real.shape)
print(y_real.shape)
d_loss1=d_model.fit(x_real,y_real)
x_fake,y_fake=cre_fake_po(g_model,vocabulary_size,input_length)
print(x_fake.shape)
print(y_fake.shape)
d_loss2=d_model.fit(x_fake,y_fake)
x_gan=generate_patien_poi(10,1285)
y_gan=np.ones((1285,1285,10))
x_gan=Reshape((1285,10))(x_gan)
y_gan=Reshape((1285,10))(y_gan)
print(x_gan.shape)
print(y_gan.shape)
g_loss=gan_model.fit(x_gan,y_gan)
d_model.save(“C:/Users/HP/Desktop/poem writer/disci.h5”)
g_model.save(“C:/Users/HP/Desktop/poem writer/generator.h5”)
lat=100
disc=disciminator((10,))
print(disc.summary())
gen=generator()
print(gen.summary())
print(generate_real_samp(64))
print(“********************************************”)
print(generate_real_samp(64)[0].shape)
print(generate_real_samp(64)[1].shape)
gan1=gan(gen,disc)
trainer(gen,disc,gan1,lat)

the error I got:

ValueError: Exception encountered when calling layer “sequential_163” (type Sequential). Input 0 of layer “sequential_161” is incompatible with the layer: expected shape=(None, 10), found shape=(None, 1285, 10, 10) Call arguments received: • inputs=tf.Tensor(shape=(None, 1285, 10), dtype=float32) • training=True • mask=None

How can I solve this problem?

submitted by /u/justahumanlearnspy
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *