Custom loss function error: TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
tallamjr opened this issue · 2 comments
tallamjr commented
This is well summarised here tensorflow/tensorflow#45079 where someone is trying to do essentially the same as is done in astronet
i.e. define a custom loss function and use this to optimise for.
When running a test function such as:
$ python astronet/t2/train.py --dataset "plasticc" --epoch 1 --batch-size 256
the follow error occurs from commit 403fcdd onwards (discoverd with git bisect
and the test defined above)
(astronet) 15:19:52 ✔ ~/github/tallamjr/origin/astronet (issues/53/load-datasets) :: python astronet/t2/train.py --dataset "plasticc" --epoch 1 --batch-size 256
[20-12-18 15:20:15] {train.py:28} INFO - =======================================================================================================================================
[20-12-18 15:20:15] {train.py:29} INFO - File Path: /Users/tallamjr/github/tallamjr/origin/astronet/astronet/t2/train.py
[20-12-18 15:20:15] {train.py:30} INFO - Parent of Directory Path: /Users/tallamjr/github/tallamjr/origin
(2991, 100, 6) (2991, 3)
[20-12-18 15:20:17] {train.py:54} INFO - None
(256, 100, 6)
Traceback (most recent call last):
File "astronet/t2/train.py", line 191, in <module>
training()
File "astronet/t2/train.py", line 126, in __call__
mode="min",
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1103, in fit
tmp_logs = self.train_function(iterator)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 808, in train_function
return step_function(self, iterator)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 798, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py", line 1259, in run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py", line 2731, in call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py", line 3420, in _call_for_each_replica
return fn(*args, **kwargs)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/autograph/impl/api.py", line 572, in wrapper
return func(*args, **kwargs)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 791, in run_step
outputs = model.train_step(data)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 759, in train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/engine/compile_utils.py", line 204, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/keras/losses.py", line 256, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/autograph/impl/api.py", line 667, in wrapper
return converted_call(f, args, kwargs, options=options)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/autograph/impl/api.py", line 393, in converted_call
return _call_unconverted(f, args, kwargs, options)
File "/usr/local/anaconda3/envs/astronet/lib/python3.7/site-packages/tensorflow/python/autograph/impl/api.py", line 478, in _call_unconverted
return f(*args, **kwargs)
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
As this is so closely related to the tensorflow issue linked above and many avenues exhausted, it is felt best to hold off until there is further updates in the thread of a solution is posted.
tallamjr commented
This has potentially been fixed with:
- for train_index, val_index in skf.split(X_train, y_train.argmax(1)):
+
+ print(type(y_train))
+ if tf.is_tensor(y_train):
+ y_train = np.array(y_train)
+ print(type(y_train))
+ y_train_split = y_train.argmax(1)
+ print(y_train_split)
+ else:
+ y_train_split = y_train.argmax(1)
+ print(y_train_split)
+
+ for train_index, val_index in skf.split(X_train, y_train_split):
X_train_cv, X_val_cv = X_train[train_index], X_train[val_index]
y_train_cv, y_val_cv = y_train[train_index], y_train[val_index]
When #56 is closed, then this can be properly tested with a new PLAsTiCC analysis being able to be run