Last active
October 11, 2019 21:20
-
-
Save netskink/bf6fc29a4c485723521ed38f11c02012 to your computer and use it in GitHub Desktop.
kernel restart error in jupyter notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from | |
# https://www.tensorflow.org/beta/tutorials/estimators/boosted_trees | |
import numpy as np | |
import pandas as pd | |
from IPython.display import clear_output | |
from matplotlib import pyplot as plt | |
import platform | |
try: | |
%tensorflow_version 2.x | |
except Exception: | |
pass | |
import tensorflow as tf | |
tf.random.set_seed(123) | |
print(platform.python_version()) | |
print(tf.version.GIT_VERSION) | |
print(tf.version.VERSION) | |
print(tf.__version__) | |
#### output of above | |
# 3.7.4 | |
# v2.0.0-rc2-26-g64c3d38 | |
# 2.0.0 | |
# 2.0.0 | |
# Load dataset. | |
dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv') | |
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv') | |
y_train = dftrain.pop('survived') | |
y_eval = dfeval.pop('survived') | |
fc = tf.feature_column | |
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town', 'alone'] | |
NUMERIC_COLUMNS = ['age', 'fare'] | |
def one_hot_cat_column(feature_name, vocab): | |
return tf.feature_column.indicator_column( | |
tf.feature_column.categorical_column_with_vocabulary_list(feature_name, vocab)) | |
feature_columns = [] | |
for feature_name in CATEGORICAL_COLUMNS: | |
# Need to one-hot encode categorical features. | |
vocabulary = dftrain[feature_name].unique() | |
feature_columns.append(one_hot_cat_column(feature_name, vocabulary)) | |
for feature_name in NUMERIC_COLUMNS: | |
feature_columns.append(tf.feature_column.numeric_column(feature_name, | |
dtype=tf.float32)) | |
example = dict(dftrain.head(1)) | |
example | |
class_fc = tf.feature_column.indicator_column( | |
tf.feature_column.categorical_column_with_vocabulary_list( | |
'class', | |
('First', 'Second', 'Third'))) | |
class_fc | |
print('Feature value: "{}"'.format(example['class'].iloc[0])) | |
print('One-hot encoded: ', tf.keras.layers.DenseFeatures([class_fc])(example).numpy()) | |
# Use entire batch since this is such a small dataset. | |
NUM_EXAMPLES = len(y_train) | |
def make_input_fn(X, y, n_epochs=None, shuffle=True): | |
def input_fn(): | |
dataset = tf.data.Dataset.from_tensor_slices((dict(X), y)) | |
if shuffle: | |
dataset = dataset.shuffle(NUM_EXAMPLES) | |
# For training, cycle thru dataset as many times as need (n_epochs=None). | |
dataset = dataset.repeat(n_epochs) | |
# In memory training doesn't use batching. | |
dataset = dataset.batch(NUM_EXAMPLES) | |
return dataset | |
return input_fn | |
# Training and evaluation input functions. | |
train_input_fn = make_input_fn(dftrain, y_train) | |
eval_input_fn = make_input_fn(dfeval, y_eval, shuffle=False, n_epochs=1) | |
# Since data fits into memory, use entire dataset per layer. It will be faster. | |
# Above one batch is defined as the entire dataset. | |
n_batches = 1 | |
est = tf.estimator.BoostedTreesClassifier(feature_columns, | |
n_batches_per_layer=n_batches) | |
# The model will stop training once the specified number of trees is built, not | |
# based on the number of steps. | |
########## | |
# In jupyter this will crash the kernel | |
############ | |
est.train(train_input_fn, max_steps=100) | |
#### output of above | |
# INFO:tensorflow:Calling model_fn. | |
# WARNING:tensorflow:From /home/davis/anaconda3/envs/py3tf2/lib/python3.7/site-packages/tensorflow_estimator/python/estimator/canned/boosted_trees.py:214: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version. | |
# Instructions for updating: | |
# Use `tf.cast` instead. | |
# INFO:tensorflow:Done calling model_fn. | |
# INFO:tensorflow:Create CheckpointSaverHook. | |
# WARNING:tensorflow:Issue encountered when serializing resources. | |
# Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore. | |
# '_Resource' object has no attribute 'name' | |
# INFO:tensorflow:Graph was finalized. | |
# INFO:tensorflow:Running local_init_op. | |
# INFO:tensorflow:Done running local_init_op. | |
# WARNING:tensorflow:Issue encountered when serializing resources. | |
# Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore. | |
# '_Resource' object has no attribute 'name' | |
# INFO:tensorflow:Saving checkpoints for 0 into /tmp/tmpokrqlmdl/model.ckpt. | |
# WARNING:tensorflow:Issue encountered when serializing resources. | |
# Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore. | |
# '_Resource' object has no attribute 'name' | |
# INFO:tensorflow:loss = 0.6931468, step = 0 | |
# WARNING:tensorflow:It seems that global step (tf.train.get_global_step) has not been increased. Current value (could be stable): 0 vs previous value: 0. You could increase the global step by passing tf.train.get_global_step() to Optimizer.apply_gradients or Optimizer.minimize. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment