probability_model
from google.colab import auth auth.authenticate_user()
CLOUD_PROJECT = 'your-project-id-here' BUCKET = 'gs://' + CLOUD_PROJECT + '-tf2-models'
!gcloud config set project $CLOUD_PROJECT
!gsutil mb $BUCKET print(BUCKET)
model.save()
fashion-mnist
probability_model.save(BUCKET + '/fashion-mnist', save_format='tf')
MODEL = 'fashion_mnist' !gcloud ai-platform models create $MODEL --regions=us-central1
VERSION = 'v1' MODEL_DIR = BUCKET + '/fashion-mnist'
!gcloud ai-platform versions create $VERSION \ --model $MODEL \ --origin $MODEL_DIR \ --runtime-version=2.1 \ --framework='tensorflow' \ --python-version=3.7
import googleapiclient.discovery def predict_json(project, model, instances, version=None): service = googleapiclient.discovery.build('ml', 'v1') name = 'projects/{}/models/{}'.format(project, model) if version is not None: name += '/versions/{}'.format(version) response = service.projects().predict( name=name, body={'instances': instances} ).execute() if 'error' in response: raise RuntimeError(response['error']) return response['predictions']
test_predictions = predict_json(CLOUD_PROJECT, MODEL, test_images[:2].tolist())
softmax
np.argmax(test_predictions[0]['softmax'])
plt.figure() plt.imshow(test_images[0]) plt.colorbar() plt.grid(False) plt.show()
feature_columns
thal
hd-prediction
model.save(BUCKET + '/hd-prediction', save_format='tf')
hd_prediction
v1
BUCKET + '/hd-prediction'
# First remove the label column test = test.pop('target') caip_instances = [] test_vals = test[:2].values for i in test_vals: example_dict = {k: [v] for k,v in zip(test.columns, i)} caip_instances.append(example_dict)
caip_instances
[{'age': [60], 'ca': [2], 'chol': [293], 'cp': [4], 'exang': [0], 'fbs': [0], 'oldpeak': [1.2], 'restecg': [2], 'sex': [1], 'slope': [2], 'thal': ['reversible'], 'thalach': [170], 'trestbps': [140]}, ...]
predict_json
test_predictions = predict_json(CLOUD_PROJECT, 'hd_prediction', caip_instances)
[{'output_1': [-1.4717596769332886]}, {'output_1': [-0.2714746594429016]}]
output_1
name
layers.Dense(1, name='prediction_probability')