<jupyter_start><jupyter_text>Tensorflow 2 on CoCalcThis shows some examples from the [migration guide](https://www.tensorflow.org/alpha/guide/migration_guide).To get TF2 in Cocalc:- Select "New Project..." → "Advanced ..." → "Custom Software" → scroll down to **"Tensorflow 2"**Hint: This project also support code formatting!<jupyter_code>import tensorflow as tf
tf.__version__
import tensorflow_datasets as tfds
W = tf.Variable(tf.ones(shape=(2, 2)), name="W")
b = tf.Variable(tf.zeros(shape=(2)), name="b")


@tf.function
def forward(x):
    return W * x + b


out_a = forward([1, 0])
print(out_a)<jupyter_output>tf.Tensor(
[[1. 0.]
 [1. 0.]], shape=(2, 2), dtype=float32)<jupyter_text>Estimator: Linear ModelShamelessly copied from https://www.tensorflow.org/alpha/tutorials/estimators/linear<jupyter_code># make matplotlib prettier
%config InlineBackend.figure_format = 'svg'
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10, 6)
import os
import sys

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.display import clear_output
from six.moves import urllib
import tensorflow.compat.v2.feature_column as fc
import tensorflow as tf
import pandas as pd<jupyter_output><empty_output><jupyter_text>Loading data reqires that your project has access to the internet<jupyter_code># 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')
dftrain.age.hist(bins=20)
dftrain.sex.value_counts().plot(kind='barh')
CATEGORICAL_COLUMNS = [
    'sex', 'n_siblings_spouses', 'parch', 'class', 'deck', 'embark_town',
    'alone'
]

NUMERIC_COLUMNS = ['age', 'fare']

feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
    vocabulary = dftrain[feature_name].unique()
    feature_columns.append(
        tf.feature_column.categorical_column_with_vocabulary_list(
            feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
    feature_columns.append(
        tf.feature_column.numeric_column(feature_name, dtype=tf.float32))
def make_input_fn(data_df,
                  label_df,
                  num_epochs=10,
                  shuffle=True,
                  batch_size=32):
    def input_function():
        ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))
        if shuffle:
            ds = ds.shuffle(1000)
        ds = ds.batch(batch_size).repeat(num_epochs)
        return ds

    return input_function


train_input_fn = make_input_fn(dftrain, y_train)
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)
ds = make_input_fn(dftrain, y_train, batch_size=10)()
for feature_batch, label_batch in ds.take(1):
    print('Some feature keys:', list(feature_batch.keys()))
    print()
    print('A batch of class:', feature_batch['class'].numpy())
    print()
    print('A batch of Labels:', label_batch.numpy())
age_column = feature_columns[7]
tf.keras.layers.DenseFeatures([age_column])(feature_batch).numpy()
gender_column = feature_columns[0]
tf.keras.layers.DenseFeatures([
    tf.feature_column.indicator_column(gender_column)
])(feature_batch).numpy()
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result)
age_x_gender = tf.feature_column.crossed_column(['age', 'sex'],
                                                hash_bucket_size=100)
derived_feature_columns = [age_x_gender]
linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns +
                                           derived_feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result)
pred_dicts = list(linear_est.predict(eval_input_fn))
probs = pd.Series([pred['probabilities'][1] for pred in pred_dicts])

probs.plot(kind='hist', bins=20, title='predicted probabilities')
from sklearn.metrics import roc_curve
from matplotlib import pyplot as plt

fpr, tpr, _ = roc_curve(y_eval, probs)
plt.plot(fpr, tpr)
plt.title('ROC curve')
plt.xlabel('false positive rate')
plt.ylabel('true positive rate')
plt.xlim(0, )
plt.ylim(0, )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12123995407577497, 'fraction_numerical': 0.010562571756601608, 'mean_word_length': 4.6571428571428575, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '295939', 'license': 'Apache-2.0', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1496, 'n_words': 297, 'path': 'tensorflow2.ipynb', 'repo_name': 'haraldschilly/tf2-binder', 'size': '4355'}