<jupyter_start><jupyter_text>Testing general kinds of neural networks<jupyter_code>from matplotlib import pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd

import keras
from keras.layers import Input, Dense
from keras.models import Model, Sequential
from keras import metrics
from keras.callbacks import History 

import tensorflow as tf
%load_ext tensorboard
x = np.random.uniform(low = -10, high = 10, size = 100)
eps = np.random.normal(loc = 0, scale = 15, size = 100)
y = -3 * x + 5 + eps
# Train-test split:
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size = 0.2, random_state = 1)
plt.scatter(x, y)
plt.show()
np.mean(y)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

lr = LinearRegression()

lr.fit(X_train.reshape(-1, 1), Y_train)

pred = lr.predict(X_train.reshape(-1, 1))

mse = mean_squared_error(X_train, pred)
pred_test = lr.predict(X_test.reshape(-1, 1))
mse_test = mean_squared_error(X_test, pred_test)


plt.figure(figsize=(20, 5))

plt.subplot(1, 2, 1)
plt.title(label=f"Train, mse: {mse}", size="20")
plt.scatter(X_train, Y_train)
plt.plot(X_train, pred)

plt.subplot(1, 2, 2)
plt.title(label=f"Test, mse: {mse_test}", size="20")
plt.scatter(X_test, Y_test)
plt.plot(X_test, pred_test)
plt.show()
model = tf.compat.v1.keras.experimental.LinearModel()
model.compile(optimizer='sgd', loss='mse')
model.fit(X_train, Y_train, epochs=500, verbose=0)
pred = model.predict(X_train)
model.summary()
mse = mean_squared_error(Y_train, pred)
pred_test = model.predict(X_test)
mse_test = mean_squared_error(Y_test, pred_test)
plt.figure(figsize=(20, 5))

plt.subplot(1,2,1)
plt.title(label=f"Dados de treino, mse: {mse}", size = 20)
plt.scatter(X_train, Y_train)
plt.plot(X_train, pred)

plt.subplot(1,2,2)
plt.title(label=f"Dados de teste, mse: {mse_test}", size = 20)
plt.scatter(X_test, Y_test)
plt.plot(X_train, pred)

plt.show()
x_2 = x**2
x_2 = np.append(x.reshape(-1, 1), x_2.reshape(-1, 1), axis=1)
print(x[0]**2)
print(x_2[0][1])
# Train-test split:
X_train, X_test, Y_train, Y_test = train_test_split(x_2, y, test_size = 0.20, random_state = 1)
X_train.shape
from keras.layers import Dense, Activation
from keras.optimizers import SGD

model = Sequential()
model.add(Dense(1, input_dim=2))
model.add(Activation('linear'))
# model.add(Dense(1, input_dim=2))
# model.add(Activation('linear'))

sgd = SGD(0.01)
model.compile(loss='mse',optimizer=sgd)

history = model.fit(X_train, Y_train, epochs=500,verbose=0)

pred = model.predict(X_train)

plt.scatter(X_train[:,0], Y_train, c='blue')
plt.plot(X_train, pred, color='b')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15125739644970415, 'fraction_numerical': 0.031804733727810654, 'mean_word_length': 6.271505376344086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '29999', 'license': 'MIT', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1103, 'n_words': 236, 'path': '02_Bruno_Salles_Pereira.ipynb', 'repo_name': 'bsallesp/keras_neural', 'size': '2704'}