3
\$\begingroup\$

This is a TensorFlow regressor that tells me that if you get x score, you will have x% to win a game in x game(game is not important).

Doing this as a project to learn tensorflow.

from __future__ import print_function

import math

from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset

tf.logging.set_verbosity(tf.logging.ERROR)
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format

stats = pd.read_csv(r"D:\Trabajo\Proyectos\Api Fort - copia\user_matches\csv_all.csv", sep=",")

my_feature = stats[['Score']]
feature_columns = [tf.feature_column.numeric_column('Score')]

targets = stats['Win']

my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0000001)
my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)

linear_regressor = tf.estimator.LinearRegressor(
    feature_columns=feature_columns,
    optimizer=my_optimizer,
    model_dir='none'
)

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):

    features = {key:np.array(value) for key,value in dict(features).items()}
    ds = Dataset.from_tensor_slices((features,targets))
    ds = ds.batch(batch_size).repeat(num_epochs)


    if shuffle:
        ds = ds.shuffle(buffer_size=10000)


    features, labels = ds.make_one_shot_iterator().get_next()
    return features, labels

    _ = linear_regressor.train(
    input_fn = lambda:my_input_fn(my_feature, targets),
    steps=1000
)

prediction_input_fn =lambda: my_input_fn(my_feature, targets, num_epochs=1, shuffle=False)

#Llamo la funcion predict() en el regresor lineal para hacer predicciones
predictions = linear_regressor.predict(input_fn=prediction_input_fn)

#Format las predicciones como un Numpy array, asi podemos calcular el error.
predictions = np.array([item['predictions'][0] for item in predictions])

##Display el mean squared error y el root mean squared error
mean_squared_error = metrics.mean_squared_error(predictions, targets)
root_mean_squared_error = math.sqrt(mean_squared_error)


min_win_value = stats['Win'].min()
max_win_value = stats['Win'].max()
min_max_difference = max_win_value - min_win_value



##revisando predicciones

calibration_data = pd.DataFrame()
calibration_data["predictions"] = pd.Series(predictions)
calibration_data["targets"] = pd.Series(targets)
calibration_data.describe()

sample = stats.sample(n=300)

x_0 = sample['Win'].min()
x_1 = sample['Win'].max()

weight = linear_regressor.get_variable_value('linear/linear_model/Score/weights')[0]
bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')

y_0 = weight * x_0 + bias
y_1 = weight * x_1 + bias

plt.plot([x_0, x_1], [y_0, y_1], c='r')

plt.ylabel("Win")
plt.xlabel("Score")

# Plot a scatter plot from our data sample.
plt.scatter(sample["Score"], sample["Win"])



def train_model(learning_rate, steps, batch_size, input_feature="Score"):
    periods = 10
    steps_per_period = steps / periods

    my_feature = input_feature
    my_feature_data = stats[[my_feature]]
    my_label = "Win"
    targets = stats[my_label]

  # Crear columnas de feature.
    feature_columns = [tf.feature_column.numeric_column(my_feature)]

  # Crear funciones de input
    training_input_fn = lambda:my_input_fn(my_feature_data, targets, batch_size=batch_size)
    prediction_input_fn = lambda: my_input_fn(my_feature_data, targets, num_epochs=1, shuffle=False)

  # Crear un objeto de regresor lineal
    my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
    my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)
    linear_regressor = tf.estimator.LinearRegressor(
      feature_columns=feature_columns,
      optimizer=my_optimizer
  )

  # Configurar un plot para que muestre el estado de nuestra linea de modelo por cada periodo.
    plt.figure(figsize=(15, 6))
    plt.subplot(1, 2, 1)
    plt.title("Learned Line by Period")
    plt.ylabel(my_label)
    plt.xlabel(my_feature)
    sample = stats.sample(n=300)
    plt.scatter(sample[my_feature], sample[my_label])
    colors = [cm.coolwarm(x) for x in np.linspace(-1, 1, periods)]

  # Entrenar el modelo, pero haciendolo en un look asi podemos revisar periodicamente.
  # Metricas de perdida
    print("Training model...")
    print("RMSE (on training data):")
    root_mean_squared_errors = []
    for period in range (0, periods):
    # Entrenar el modelo, empezando desde el punto anterior
        linear_regressor.train(
            input_fn=training_input_fn,
            steps=steps_per_period
    )
    # Computando predicciones
    predictions = linear_regressor.predict(input_fn=prediction_input_fn)
    predictions = np.array([item['predictions'][0] for item in predictions])

    # Computando perdidas
    root_mean_squared_error = math.sqrt(
        metrics.mean_squared_error(predictions, targets))
    # Ocasionalmente print la perdida

    # Agregar la perdida de metrica de este periodo a nuestra lista
    root_mean_squared_errors.append(root_mean_squared_error)
    # Finalmente rastrearemos los weights y biases over time.
    # Aplicar matematica para asegurarnos de que la data y la linea estan bien graficadas
    y_extents = np.array([0, sample[my_label].max()])

    weight = linear_regressor.get_variable_value('linear/linear_model/%s/weights' % input_feature)[0]
    bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')

    x_extents = (y_extents - bias) / weight
    x_extents = np.maximum(np.minimum(x_extents,
                                      sample[my_feature].max()),
                           sample[my_feature].min())
    y_extents = weight * x_extents + bias
    plt.plot(x_extents, y_extents, color=colors[period])


  # Mostrar una graph de las perdidas sobre los periodos
    plt.subplot(1, 2, 2)
    plt.ylabel('RMSE')
    plt.xlabel('Periods')
    plt.title("Root Mean Squared Error vs. Periods")
    plt.tight_layout()
    plt.plot(root_mean_squared_errors)

  # Mostrar tabla con data de prediccion
    calibration_data = pd.DataFrame()
    calibration_data["predictions"] = pd.Series(predictions)
    calibration_data["targets"] = pd.Series(targets)
    display.display(calibration_data.describe())


    train_model(
    learning_rate=0.000001,
    steps=100000,
    batch_size=20
)

If you have any comments or suggestions, feel free to comment.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.