valid_cnt = int(train.shape[0] * 0.1) test_idx, training_idx = indices[:valid_cnt],\ indices[valid_cnt:] test, train = train[test_idx,:],\ train[training_idx,:] test_labels, train_labels = labels[test_idx],\ labels[training_idx] sess = tf.InteractiveSession() # Logistic Regression # steps is number of total batches # steps/batch_size = num_epochs classifier = skflow.TensorFlowLinearClassifier( n_classes=5, steps=1000, 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,
from tensorflow.examples.tutorials.mnist import input_data from tensorflow.contrib import skflow ### Download and load MNIST data. #mnist = skflow.datasets.load_dataset('mnist') from tensorflow.examples.tutorials.mnist import input_data ### Download and load MNIST data. mnist = input_data.read_data_sets('MNIST_data') ### Linear classifier. classifier = skflow.TensorFlowLinearClassifier( n_classes=10, batch_size=100, steps=1000, learning_rate=0.01) classifier.fit(mnist.train.images, mnist.train.labels) score = metrics.accuracy_score(mnist.test.labels, classifier.predict(mnist.test.images)) print('Accuracy: {0:f}'.format(score)) ### Convolutional network def max_pool_2x2(tensor_in): return tf.nn.max_pool(tensor_in, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') def conv_model(X, y): # reshape X to 4d tensor with 2nd and 3rd dimensions being image width and height # final dimension being the number of color channels X = tf.reshape(X, [-1, 28, 28, 1]) # first conv layer will compute 32 features for each 5x5 patch
# 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. import shutil from sklearn import datasets, metrics, cross_validation from tensorflow.contrib import skflow iris = datasets.load_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) classifier = skflow.TensorFlowLinearClassifier(n_classes=3) classifier.fit(X_train, y_train) score = metrics.accuracy_score(y_test, classifier.predict(X_test)) print('Accuracy: {0:f}'.format(score)) # Clean checkpoint folder if exists try: shutil.rmtree('/tmp/skflow_examples/iris_custom_model') except OSError: pass # Save model, parameters and learned variables. classifier.save('/tmp/skflow_examples/iris_custom_model') classifier = None ## Restore everything