In [ ]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [ ]:
sigmoid = lambda x: 1 / (1 + np.exp(-x))
x=np.linspace(-5,5,50)
plt.plot(x,sigmoid(x),'b')
plt.grid()
In [ ]:
# softmax
In [ ]:
softmax = lambda W: np.exp(np.array(W)) / np.sum(np.exp(np.array(W)))
In [ ]:
x=np.linspace(-5,5,50)
W=softmax(x)
plt.plot(x,W,'b')
plt.grid()
In [ ]:
# ReLU
In [ ]:
ReLU = lambda w: np.maximum(w,0)
In [ ]:
x=np.linspace(-5,5,50)
plt.plot(x,ReLU(x),'b')
plt.grid()
In [ ]:
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import numpy as np
import tensorflow as tf
%matplotlib inline
In [ ]:
dados_df = pd.read_csv('pesos-alturas.csv',decimal=',')
In [ ]:
dados_df.head()
In [ ]:
X_train, X_test, y_train, y_test = train_test_split(dados_df[['Alturas']],dados_df[['Pesos']],
test_size=0.33)
In [ ]:
# Definindo os coeficientes (aquilo que queremos calcular):
# y = ax + b
In [ ]:
a = tf.Variable(np.random.randn(), name="ca")
b = tf.Variable(np.random.randn(), name="cl")
In [ ]:
# Definindo as variáveis (dependente e independente):
In [ ]:
x = tf.placeholder("float")
y = tf.placeholder("float")
In [ ]:
# Definindo o modelo:
In [ ]:
y_hat = tf.add(tf.multiply(x, a), b)
In [ ]:
# Definindo a função de perda:
In [ ]:
perda = tf.reduce_sum(tf.pow(y_hat - y, 2)/2)
In [ ]:
# Otimização:
In [ ]:
l_rate = 0.001
In [ ]:
otim = tf.train.GradientDescentOptimizer(l_rate).minimize(perda)
init = tf.global_variables_initializer()
In [ ]:
# Vamos rodar. Vou repetir o treino para evitar o problema do "mínimo local":
In [ ]:
epochs = 10000
with tf.Session() as sess:
sess.run(init)
print('a',sess.run(a),'b',sess.run(b))
for epoch in range(epochs):
sess.run(otim, feed_dict={x: X_train, y: y_train})
plt.scatter(X_train, y_train)
print('a',sess.run(a),'b',sess.run(b))
plt.plot(X_train, sess.run(a) * X_train + sess.run(b),c="r")
In [1]:
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
import itertools as ite
import tensorflow as tf
%matplotlib inline
np.random.seed(42)
X = np.linspace(1.5,3.0,num=100)
Y = np.array([x**4 + (np.random.rand()*8.5) for x in X])
Z = np.array([(X[i]*Y[i]) + (np.random.rand()*10.2) for i in range(0,100)])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X, Y, Z, c='r',s=8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
Out[1]:
In [2]:
features = pd.DataFrame({'X':X, 'Z':Z})
In [3]:
labels = pd.DataFrame({'Y':Y})
In [4]:
features.head()
Out[4]:
In [5]:
labels.head()
Out[5]:
In [6]:
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.33, random_state=42)
In [7]:
feature_cols = [tf.contrib.layers.real_valued_column('X'),tf.contrib.layers.real_valued_column('Z')]
In [34]:
regressor = tf.contrib.learn.DNNRegressor(
feature_columns=feature_cols, hidden_units=[4],label_dimension=1)
In [35]:
input_func = tf.estimator.inputs.pandas_input_fn(X_train,y_train,batch_size=4,num_epochs=None,shuffle=True)
In [36]:
regressor.fit(input_fn=input_func,steps=1000)
Out[36]:
In [37]:
eval_func = tf.estimator.inputs.pandas_input_fn(X_test,y_test,batch_size=4,num_epochs=None,shuffle=True)
In [38]:
resultado = regressor.evaluate(input_fn=eval_func,steps=1)
In [39]:
print('Perda',resultado['loss'])
In [40]:
y_hat = regressor.predict(input_fn=eval_func)
In [47]:
preds = np.array(list(ite.islice(y_hat,int(X_test['X'].size))))
In [49]:
fig = plt.figure()
ax = ax = fig.add_subplot(111, projection='3d')
ax.scatter(X, Y, Z, c='r',s=8)
ax.scatter(X_test['X'], preds, X_test['Z'], c='k', marker='*',s=100)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
Out[49]:
In [ ]: