HW 2 Training NN on random Gaussians and Lorentzian

94 views
Skip to first unread message

Pratik Patnaik

unread,
May 19, 2020, 2:29:31 AM5/19/20
to Machine Learning for Physicists
# keras:
from tensorflow.keras import Sequential # Sequential is the neural-network class
from tensorflow.keras.layers import Dense # Dense is the standard network layer

# array math:
import numpy as np

# plotting:
import matplotlib.pyplot as plt
import matplotlib

# setting up a network
N = 1000
net=Sequential()
net.add(Dense(30, input_shape=(N,), activation='relu'))
net.add(Dense(2, activation='softmax'))
net.compile(loss='mean_squared_error',
              optimizer='adam')
# defining required functions 
def lorentzian( x, x0, a, gam ):
    return a * gam**2 / ( gam**2 + ( x - x0 )**2)
def gaussian(x, mu, sig):
    return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
inpoot = np.linspace(-50,50,N)
def gen_sample(): # generates a random gaussian or lorentzian
    s = np.random.randint(2)
    if s == 1:
        return gaussian(inpoot,np.random.uniform(-5,5),np.random.uniform(-10,10)),[0,1]
    else :
        return lorentzian (inpoot,np.random.uniform(-5,5),np.random.uniform(-10,10),np.random.uniform(-10,10)),[1,0]

#Training
training_batches=100
batchsize=N
costs=np.zeros(training_batches) 

for j in range(training_batches):
    y_in=gen_sample()[0] 
    y_target=gen_sample()[1] 
    costs[j]=net.train_on_batch(y_in,y_target) 
    print(str(costs[j]),end="   \r") 
    

S=gen_sample()
y_out=Net.predict_on_batch(S[0]) 
print(S[1])
# plot it!
plt.plot(inpoot,S[0],)
plt.legend()
plt.show()


With this code, I am getting the following error.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-85abaed28aa5> in <module>
      6     y_in=gen_sample()[0]
      7     y_target=gen_sample()[1]
----> 8     costs[j]=net.train_on_batch(y_in,y_target)
      9     print(str(costs[j]),end="   \r")
     10 

~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics, return_dict)
   1346                                                     class_weight)
   1347       train_function = self.make_train_function()
-> 1348       logs = train_function(iterator)
   1349 
   1350     if reset_metrics:

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    578         xla_context.Exit()
    579     else:
--> 580       result = self._call(*args, **kwds)
    581 
    582     if tracing_count == self._get_tracing_count():

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    625       # This is the first call of __call__, so we have to initialize.
    626       initializers = []
--> 627       self._initialize(args, kwds, add_initializers_to=initializers)
    628     finally:
    629       # At this point we know that the initialization is complete (or less

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    503     self._graph_deleter = FunctionDeleter(self._lifted_initializer_graph)
    504     self._concrete_stateful_fn = (
--> 505         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
    506             *args, **kwds))
    507 

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2444       args, kwargs = None, None
   2445     with self._lock:
-> 2446       graph_function, _, _ = self._maybe_define_function(args, kwargs)
   2447     return graph_function
   2448 

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   2775 
   2776       self._function_cache.missed.add(call_context_key)
-> 2777       graph_function = self._create_graph_function(args, kwargs)
   2778       self._function_cache.primary[cache_key] = graph_function
   2779       return graph_function, args, kwargs

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   2655     arg_names = base_arg_names + missing_arg_names
   2656     graph_function = ConcreteFunction(
-> 2657         func_graph_module.func_graph_from_py_func(
   2658             self._name,
   2659             self._python_function,

~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    979         _, original_func = tf_decorator.unwrap(python_func)
    980 
--> 981       func_outputs = python_func(*func_args, **func_kwargs)
    982 
    983       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~/.local/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    439         # __wrapped__ allows AutoGraph to swap in a converted function. We give
    440         # the function a weak reference to itself to avoid a reference cycle.
--> 441         return weak_wrapped_fn().__wrapped__(*args, **kwds)
    442     weak_wrapped_fn = weakref.ref(wrapped_fn)
    443 

~/.local/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

ValueError: in user code:

    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
        outputs = self.distribute_strategy.run(
    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:531 train_step  **
        y_pred = self(x, training=True)
    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:885 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs,
    /home/prothicc/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/input_spec.py:212 assert_input_compatibility
        raise ValueError(

    ValueError: Input 0 of layer sequential_5 is incompatible with the layer: expected axis -1 of input shape to have value 1000 but received input with shape [1000, 1]


Can someone help me with this?
Thanks

Pablo Piskunow

unread,
May 24, 2020, 7:18:12 AM5/24/20
to Machine Learning for Physicists
There seems to be a problem with the shape of the date.

Probably check this line
net.add(Dense(30, input_shape=(N,), activation='relu'))

Also I see other possible issues here:


for j in range(training_batches):
    y_in=gen_sample()[0] 
    y_target=gen_sample()[1] 
    costs[j]=net.train_on_batch(y_in,y_target) 
    print(str(costs[j]),end="   \r")


Each call of `gen_sample()` will generate a new lorentzian or gaussian, and you call it two times.
So the networkwill compare two different things.


Also, check that the output of gen_sample()[1] is what you want.



In the definition of gen_sample(), there is an extra space (!)
`return lorentzian (`.

Pratik Patnaik

unread,
May 24, 2020, 11:06:46 AM5/24/20
to Machine Learning for Physicists
Hello Pablo,
It turns out I didn't generate multiple random samples for the network to train on in each loop. My function just returned a single input and output. I changed it suitably to generate multiple samples in each run (added "batchsize" as a parameter), and the program went well. Thank you for helping me out.
Cheers,
Pratik
Reply all
Reply to author
Forward
0 new messages