from google.colab import authauth.authenticate_user()
CLOUD_PROJECT = 'your-project-id-here'BUCKET = 'gs://' + CLOUD_PROJECT + '-tf2-models'
!gcloud config set project $CLOUD_PROJECT
!gsutil mb $BUCKETprint(BUCKET)
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.discoverydef 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())
np.argmax(test_predictions[0]['softmax'])
plt.figure()plt.imshow(test_images[0])plt.colorbar()plt.grid(False)plt.show()
# First remove the label columntest = test.pop('target') caip_instances = []test_vals = test[:2].valuesfor i in test_vals: example_dict = {k: [v] for k,v in zip(test.columns, i)} caip_instances.append(example_dict)
[{'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]},...]
test_predictions = predict_json(CLOUD_PROJECT, 'hd_prediction', caip_instances)
[{'output_1': [-1.4717596769332886]}, {'output_1': [-0.2714746594429016]}]
layers.Dense(1, name='prediction_probability')