how can i change using Tensorflow code for finding solution?

Viewed 53

when i studied in Tensorflow, I got follow a error code.

I tried to change the activation function and learning rate in various ways, but I couldn't solve the problem. How do I change the code? (I am running follow code on jupyter notebook)

Imports & Settings

import warnings
warnings.filterwarnings('ignore')
%matplotlib inline

from pathlib import Path
from copy import deepcopy
import numpy as np
import pandas as pd

import sklearn
from sklearn.datasets import make_circles # To generate the dataset

import keras
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras import optimizers
from tensorflow.keras.callbacks import TensorBoard

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from mpl_toolkits.mplot3d import Axes3D  # 3D plots

import seaborn as sns
results_path = Path('results')
if not results_path.exists():
    results_path.mkdir()

Input Data(generate random data)

N = 50000
factor = 0.1
noise = 0.1

X, y = make_circles(
    n_samples=N,
    shuffle=True,
    factor=factor,
    noise=noise)

Y = np.zeros((N, 2))
for c in [0, 1]:
    Y[y == c, c] = 1

Build Keras Model

model = Sequential([
    Dense(units=3, input_shape=(2,), name='hidden'),
    Activation('sigmoid', name='logistic'),
    Dense(2, name='output'),
    Activation('softmax', name='softmax')
])

Compile Model

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

tb_callback = TensorBoard(log_dir=results_path / 'tensorboard', 
                          histogram_freq=1, 
                          write_graph=True, 
                          write_images=True)

Train Model

training=model.fit(X, 
          Y, 
          epochs=50,
          validation_split=.2,
          batch_size=128, 
          verbose=1, 
          callbacks=[tb_callback])
Epoch 1/50
   2/4000 [..............................] - ETA: 15:09 - loss: nan - accuracy: 0.3500WARNING:tensorflow:Callbacks method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0020s vs `on_train_batch_end` time: 0.4538s). Check your callbacks.
3990/4000 [============================>.] - ETA: 0s - loss: nan - accuracy: 0.4982
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_740/2904982164.py in <module>
----> 1 training=model.fit(X, 
      2           Y,
      3           epochs=50,
      4           validation_split=.2,
      5           batch_size=10,

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
    106   def _method_wrapper(self, *args, **kwargs):
    107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
--> 108       return method(self, *args, **kwargs)
    109 
    110     # Running inside `run_distribute_coordinator` already.

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1135           epoch_logs.update(val_logs)
   1136 
-> 1137         callbacks.on_epoch_end(epoch, epoch_logs)
   1138         training_logs = epoch_logs
   1139         if self.stop_training:

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\keras\callbacks.py in on_epoch_end(self, epoch, logs)
    410     for callback in self.callbacks:
    411       if getattr(callback, '_supports_tf_logs', False):
--> 412         callback.on_epoch_end(epoch, logs)
    413       else:
    414         if numpy_logs is None:  # Only convert once.

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\keras\callbacks.py in on_epoch_end(self, epoch, logs)
   2180 
   2181     if self.histogram_freq and epoch % self.histogram_freq == 0:
-> 2182       self._log_weights(epoch)
   2183 
   2184     if self.embeddings_freq and epoch % self.embeddings_freq == 0:

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\keras\callbacks.py in _log_weights(self, epoch)
   2232           for weight in layer.weights:
   2233             weight_name = weight.name.replace(':', '_')
-> 2234             summary_ops_v2.histogram(weight_name, weight, step=epoch)
   2235             if self.write_images:
   2236               self._log_weight_as_image(weight, weight_name, epoch)

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\ops\summary_ops_v2.py in histogram(name, tensor, family, step)
    834         name=scope)
    835 
--> 836   return summary_writer_function(name, tensor, function, family=family)
    837 
    838 

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\ops\summary_ops_v2.py in summary_writer_function(name, tensor, function, family)
    762     return control_flow_ops.no_op()
    763   with ops.device("cpu:0"):
--> 764     op = smart_cond.smart_cond(
    765         should_record_summaries(), record, _nothing, name="")
    766     if not context.executing_eagerly():

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\framework\smart_cond.py in smart_cond(pred, true_fn, false_fn, name)
     52   if pred_value is not None:
     53     if pred_value:
---> 54       return true_fn()
     55     else:
     56       return false_fn()

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\ops\summary_ops_v2.py in record()
    756     with ops.name_scope(name_scope), summary_op_util.summary_scope(
    757         name, family, values=[tensor]) as (tag, scope):
--> 758       with ops.control_dependencies([function(tag, scope)]):
    759         return constant_op.constant(True)
    760 

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\ops\summary_ops_v2.py in function(tag, scope)
    827   def function(tag, scope):
    828     # Note the identity to move the tensor to the CPU.
--> 829     return gen_summary_ops.write_histogram_summary(
    830         _summary_state.writer._resource,  # pylint: disable=protected-access
    831         _choose_step(step),

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\ops\gen_summary_ops.py in write_histogram_summary(writer, step, tag, values, name)
    476       pass
    477     try:
--> 478       return write_histogram_summary_eager_fallback(
    479           writer, step, tag, values, name=name, ctx=_ctx)
    480     except _core._SymbolicException:

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\ops\gen_summary_ops.py in write_histogram_summary_eager_fallback(writer, step, tag, values, name, ctx)
    495   _inputs_flat = [writer, step, tag, values]
    496   _attrs = ("T", _attr_T)
--> 497   _result = _execute.execute(b"WriteHistogramSummary", 0, inputs=_inputs_flat,
    498                              attrs=_attrs, ctx=ctx, name=name)
    499   _result = None

~\.conda\envs\ml4t\lib\site-packages\tensorflow\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57   try:
     58     ctx.ensure_initialized()
---> 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:

main reason

InvalidArgumentError: Nan in summary histogram for: hidden/kernel_0 [Op:WriteHistogramSummary]
1 Answers

I replicated the same issue in jupyter notebook using Tensorflow 2.7. Please find below changes:

# Load the TensorBoard notebook extension
%load_ext tensorboard

# Clear any logs from previous runs
!rm -rf ./logs/

Then do a little changes in below code:

log_dir = "logs/results_path/"
tb_callback = TensorBoard(log_dir=log_dir, 
                          histogram_freq=1, 
                          write_graph=True, 
                          write_images=True)

Again train the model by fitting the dataset and tensorboard callback.

training=model.fit(X, Y, epochs=50, validation_split=.2,
          batch_size=128, verbose=1, callbacks=[tb_callback])

Output:

Epoch 46/50
313/313 [==============================] - 1s 3ms/step - loss: 0.0897 - accuracy: 0.9986 - val_loss: 0.0852 - val_accuracy: 0.9984
Epoch 47/50
313/313 [==============================] - 1s 2ms/step - loss: 0.0822 - accuracy: 0.9989 - val_loss: 0.0781 - val_accuracy: 0.9992
Epoch 48/50
313/313 [==============================] - 1s 2ms/step - loss: 0.0755 - accuracy: 0.9991 - val_loss: 0.0719 - val_accuracy: 0.9993
Epoch 49/50
313/313 [==============================] - 1s 2ms/step - loss: 0.0693 - accuracy: 0.9994 - val_loss: 0.0660 - val_accuracy: 0.9993
Epoch 50/50
313/313 [==============================] - 1s 3ms/step - loss: 0.0636 - accuracy: 0.9995 - val_loss: 0.0606 - val_accuracy: 0.9994

Start TensorBoard within a notebook.

%tensorboard --logdir logs/results_path 

Output:

enter image description here

Related