<jupyter_start><jupyter_text>Import libraries<jupyter_code>import os
import numpy as np
from matplotlib import pyplot as plt
import torch
from torch import nn

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(device)<jupyter_output>cuda:0<jupyter_text>Models<jupyter_code>from gan import TemporalNetwork, BarGenerator, MuseCritic, MuseGenerator<jupyter_output><empty_output><jupyter_text>TempNetwork<jupyter_code>tempnet = TemporalNetwork()
x = torch.rand(10, 32)
tempnet(x).shape
print('Number of parameters:', sum(p.numel() for p in tempnet.parameters()))<jupyter_output>Number of parameters: 101472<jupyter_text>BarGenerator<jupyter_code>bargenerator = BarGenerator()
a = torch.rand(10, 128)
bargenerator(a).shape
print('Number of parameters:', sum(p.numel() for p in bargenerator.parameters()))<jupyter_output>Number of parameters: 1517313<jupyter_text>MuseGenerator<jupyter_code>generator = MuseGenerator()

cords = torch.rand(10, 32)
style = torch.rand(10, 32)
melody = torch.rand(10, 4, 32)
groove = torch.rand(10, 4, 32)

generator(cords, style, melody, groove).shape
print('Number of parameters:', sum(p.numel() for p in generator.parameters()))<jupyter_output>Number of parameters: 6576612<jupyter_text>MuseCritic<jupyter_code>critic = MuseCritic()
a = torch.rand(10, 4, 2, 16, 84)
critic(a).shape
print('Number of parameters:', sum(p.numel() for p in critic.parameters()))<jupyter_output>Number of parameters: 1446401<jupyter_text>Dataset Loader<jupyter_code>from torch.utils.data import DataLoader
from data.utils import MidiDataset

dataset = MidiDataset(path='data/chorales/Jsb16thSeparated.npz')
dataloader = DataLoader(dataset, batch_size=64, shuffle=True, drop_last=True)<jupyter_output><empty_output><jupyter_text>Define Models  Generator<jupyter_code>generator = MuseGenerator(z_dimension=32, hid_channels=1024, hid_features=1024, out_channels=1).to(device)
g_optimizer = torch.optim.Adam(generator.parameters(), lr=0.001, betas=(0.5, 0.9))<jupyter_output><empty_output><jupyter_text>Critic<jupyter_code>critic = MuseCritic(hid_channels=128,
                    hid_features=1024,
                    out_features=1).to(device)
c_optimizer = torch.optim.Adam(critic.parameters(), lr=0.001, betas=(0.5, 0.9))
from gan import initialize_weights

generator = generator.apply(initialize_weights)
critic = critic.apply(initialize_weights)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>from trainer import Trainer
trainer = Trainer(generator, critic, g_optimizer, c_optimizer)
trainer.train(dataloader, epochs=1000)
losses = trainer.data.copy()<jupyter_output><empty_output><jupyter_text>Save losses<jupyter_code>import pandas as pd

df = pd.DataFrame.from_dict(losses)
#df.to_csv('results.csv', index=False)<jupyter_output><empty_output><jupyter_text>Save models<jupyter_code># generator = generator.eval().cpu()
# critic = critic.eval().cpu()
#torch.save(generator, 'generator_e1000.pt')
#torch.save(critic, 'critic_e1000.pt')<jupyter_output><empty_output><jupyter_text>Plot losses<jupyter_code>plt.figure(figsize=(12, 8))
plt.plot(losses['gloss'][:500], 'orange', label='generator')
plt.plot(losses['cfloss'][:500], 'r', label='critic fake')
plt.plot(losses['crloss'][:500], 'g', label='critic real')
plt.plot(losses['cploss'][:500], 'b', label='critic penalty')
plt.plot(losses['closs'][:500], 'm', label='critic')
plt.xlabel('epoch', fontsize=12)
plt.ylabel('loss', fontsize=12)
plt.grid()
plt.legend()
plt.show()
#plt.savefig('losses.png')
generator = generator.eval().cpu()
#generator = torch.load('generator_e1000.pt')<jupyter_output><empty_output><jupyter_text>Make prediction<jupyter_code>chords = torch.rand(1, 32)
style = torch.rand(1, 32)
melody = torch.rand(1, 4, 32)
groove = torch.rand(1, 4, 32)

preds = generator(chords, style, melody, groove).detach()<jupyter_output><empty_output><jupyter_text>Get music data<jupyter_code>from data.utils import postProcess

preds = preds.numpy()
music_data = postProcess(preds)<jupyter_output><empty_output><jupyter_text>Save file<jupyter_code>filename = 'myexample.midi'
music_data.write('midi', fp=filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14540202214732786, 'fraction_numerical': 0.03731343283582089, 'mean_word_length': 8.940191387559809, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '252024', 'license': 'MIT', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1510, 'n_words': 248, 'path': 'demo/demo.ipynb', 'repo_name': 'akanametov/MuseGAN', 'size': '4154'}