Esempio n. 1
0
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from sklearn import metrics, cross_validation

from tensorflow.contrib import skflow

# Load dataset.
iris = skflow.datasets.load_dataset('iris')
X_train, X_test, y_train, y_test = cross_validation.train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42)

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                            n_classes=3,
                                            steps=200)

# Fit and predict.
classifier.fit(X_train, y_train)
score = metrics.accuracy_score(y_test, classifier.predict(X_test))
print('Accuracy: {0:f}'.format(score))
            optimizer='Adam',
            learning_rate=0.01,
            continue_training=True)

# One line training
classifier.fit(train.reshape([-1,36*36]),train_labels)

# sklearn compatible accuracy
sklearn.metrics.accuracy_score(test_labels,
        classifier.predict(test.reshape([-1,36*36])))

# Dense neural net
classifier = skflow.TensorFlowDNNClassifier(
            hidden_units=[10,5],
            n_classes=5,
            steps=1000,
            optimizer='Adam',
            learning_rate=0.01,
            continue_training=True)
classifier.fit(train.reshape([-1,36*36]),train_labels)

# simple accuracy
sklearn.metrics.accuracy_score(test_labels,
        classifier.predict(test.reshape([-1,36*36])))

# confusion is easy
conf = metrics.confusion_matrix(train_labels,
        classifier.predict(train.reshape([-1,36*36])))
print(conf)

### Convolutional net
#  limitations under the License.

from sklearn import datasets, metrics
from sklearn.cross_validation import train_test_split

import tensorflow as tf
from tensorflow.contrib import skflow

iris = datasets.load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data,
                                                    iris.target,
                                                    test_size=0.2,
                                                    random_state=42)


# setup exponential decay function
def exp_decay(global_step):
    return tf.train.exponential_decay(learning_rate=0.1,
                                      global_step=global_step,
                                      decay_steps=100,
                                      decay_rate=0.001)


# use customized decay function in learning_rate
classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                            n_classes=3,
                                            steps=800,
                                            learning_rate=exp_decay)
classifier.fit(X_train, y_train)
score = metrics.accuracy_score(y_test, classifier.predict(X_test))
Esempio n. 4
0
"""

import numpy as np
import pandas as pd
from sklearn import metrics, cross_validation
from tensorflow.contrib import skflow
import os
import random

random.seed(42)

filename = filename = os.getcwd() + '/datasets/mesothelioma.csv'

dataset = pd.read_csv(filename, low_memory=False)
dataset = dataset.drop(['city'], axis=1)
dataset['class of diagnosis'] = np.where(dataset['class of diagnosis'] == 1, 0,
                                         1)
data = dataset.drop(['class of diagnosis'], axis=1)
target = dataset['class of diagnosis']
number_of_classes = len(np.unique(target))  #2

xtrain, xtest, ytrain, ytest = cross_validation.train_test_split(
    data, target, test_size=0.2, random_state=20)

classifier = skflow.TensorFlowDNNClassifier(hidden_units=[40, 60, 40],
                                            n_classes=number_of_classes,
                                            steps=2000,
                                            learning_rate=.1)
classifier.fit(xtrain, ytrain)
score = metrics.accuracy_score(ytest, classifier.predict(xtest))
print('Accuracy: {0:f}'.format(score))
Esempio n. 5
0
                                                    test_size=0.2,
                                                    random_state=42)

X_train, X_val, y_train, y_val = train_test_split(X_train,
                                                  y_train,
                                                  test_size=0.2,
                                                  random_state=42)
val_monitor = skflow.monitors.ValidationMonitor(X_val,
                                                y_val,
                                                print_steps=100,
                                                early_stopping_rounds=200,
                                                n_classes=3)

# classifier with early stopping on training data
classifier1 = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                             n_classes=3,
                                             steps=10)
classifier1.fit(X_train, y_train)
score1 = metrics.accuracy_score(y_test, classifier1.predict(X_test))

# classifier with early stopping on validation data
classifier2 = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                             n_classes=3,
                                             steps=1000)
classifier2.fit(X_train, y_train, val_monitor)
score2 = metrics.accuracy_score(y_test, classifier2.predict(X_test))

# in many applications, the score is improved by using early stopping on val data
print(
    'test score after {} steps {}, after {} steps (early stopping) {}'.format(
        classifier1._monitor.steps, score1, classifier2._monitor.steps,
                                                  labels,
                                                  test_size=0.2,
                                                  random_state=42)

# Remove this exit to move on
# exit()
'''
Part 4 : Simple DNN
------------------------------------------------------------------------------------------------------------------
'''
# Now we'll create a simple deep neural network tensorflow graph
# For regression you can use learn.TensorFlowDNNRegressor
classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                           n_classes=y_classes,
                                           batch_size=32,
                                           steps=100,
                                           optimizer="Adam",
                                           learning_rate=0.01,
                                           dropout=0.6)

# Here we'll train our DNN
classifier.fit(X_train, y_train, logdir='dnnLogs')

# and evaluate it on our dev data
predictions = classifier.predict(X_dev)
score = metrics.accuracy_score(y_dev, predictions)
print("Accuracy: %f" % score)

# Remove this exit to move on
# exit()
'''