Toy Dataset 4 way


In [ ]:
# Make 10k examples
import tflearn
import tensorflow as tf
examples = 15000
data = pd.DataFrame(np.random.randn(examples, 2), columns=['x1', 'x2'])

In [ ]:
from tensorflow.python.client import device_lib

def get_available_gpus():
    local_device_protos = device_lib.list_local_devices()
    return [x.name for x in local_device_protos if x.device_type == 'GPU']

In [ ]:
labels = pd.DataFrame(
    np.where((data['x1'] + data['x2']) > 0, 1, 0), columns=['blue'])
labels['red'] = np.where(labels['blue'] == 1, 0, 1)
labels['green'] = np.where((data['x1'] > 1) & (data['x2'] > 1.5), 1, 0)
labels['yellow'] = np.where((data['x1']**2 + -data['x2']**3 - 4 > 0), 1, 0)
labels.loc[labels[labels['yellow'] == 1].index, ['blue', 'red', 'green']] = 0
labels.loc[labels[labels['green'] == 1].index, ['blue', 'red']] = 0

In [ ]:
# Check that
labels.sum(axis=1).value_counts(dropna=False)

In [ ]:
df = pd.concat([data, labels], axis=1)

In [ ]:
fig, ax = plt.subplots()
colors = pd.Series(np.where(df['blue'] == 1, 1, np.where(
    df['red'] == 1, 0, np.where(df['green'] == 1, 2, 3))))
colors_map = {1: 'blue', 0: 'red', 2: 'green', 3: 'yellow'}
ax.scatter(df['x1'], df['x2'], c=colors.apply(lambda x: colors_map[x]))
plt.show()

In [ ]:
from sklearn.model_selection import train_test_split

In [ ]:
train, test = train_test_split(df)
train_x, train_y = train[['x1', 'x2']].values, train[[
    'blue', 'red', 'green', 'yellow'
]].values
test_x, test_y = test[['x1',
                       'x2']].values, test[['blue', 'red', 'green',
                                            'yellow']].values

In [ ]:
net = tflearn.input_data(shape=[None, 2])
net = tflearn.fully_connected(net, 1000, activation='relu')
net = tflearn.fully_connected(net, 1000, activation='relu')
net = tflearn.fully_connected(net, 1000, activation='relu')
# net = tflearn.fully_connected(net, 1000, activation='relu')
net = tflearn.fully_connected(net, 4, activation='softmax')
net = tflearn.regression(net, learning_rate=.001)
# Define model
model = tflearn.DNN(net, tensorboard_verbose=3)

See how model is predicting


In [ ]:
model.fit?

In [ ]:
tflearn.regression?

In [ ]:
def train_and_show_figs():
    model.fit(train_x, train_y, n_epoch=10, batch_size=640, show_metric=True)
    indicies = np.argmax(model.predict(test_x), axis=1)
    classes = np.zeros([test_x.shape[0], 4])
    classes[np.arange(indicies.size), indicies] = 1
    test_predictions = pd.concat(
    [
        pd.DataFrame(test_x, columns=['x1', 'x2']),
        pd.DataFrame(
            classes, columns=['blue', 'red', 'green', 'yellow'])
    ],
    axis=1)
    fig, ax = plt.subplots(1,2)
    colors = pd.Series(np.where(test_predictions['blue'] == 1, 1, np.where(
        test_predictions['red'] == 1, 0, np.where(test_predictions['green'] == 1, 2, 3))))
    ax[0].scatter(test_predictions['x1'], test_predictions['x2'], c=colors.apply(lambda x: colors_map[x]))
    ax[0].set_title('predictions')
    colors = pd.Series(np.where(test['blue'] == 1, 1, np.where(
        test['red'] == 1, 0, np.where(test['green'] == 1, 2, 3))))
    ax[1].scatter(test['x1'], test['x2'], c=colors.apply(lambda x: colors_map[x]))
    ax[1].set_title('actuals')
    plt.show()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]:
train_and_show_figs()

In [ ]: