AttributeError: /home/hp/anaconda3/lib/libxgboost.so: undefined symbol: XGDMatrixSetDenseInfo - python-3.x

I have installed XGBoost with pip3.
While trying to run this line:
clf = GridSearchCV(estimator=xgb.XGBClassifier(use_label_encoder =False), param_grid=params, scoring = 'accuracy', cv=20).fit(data_train, label_train)
I get this error :
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-90-718a4a11a2ba> in <module>
4 startTime = time.time()
5
----> 6 clf = GridSearchCV(estimator=xgb.XGBClassifier(use_label_encoder =False), param_grid=params, scoring = 'accuracy', cv=20).fit(data_train, label_train)
7 print(clf.best_estimator_)
8 print('--------------------------------------------------')
~/anaconda3/lib/python3.8/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
61 extra_args = len(args) - len(all_args)
62 if extra_args <= 0:
---> 63 return f(*args, **kwargs)
64
65 # extra_args > 0
~/anaconda3/lib/python3.8/site-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params)
878 refit_start_time = time.time()
879 if y is not None:
--> 880 self.best_estimator_.fit(X, y, **fit_params)
881 else:
882 self.best_estimator_.fit(X, **fit_params)
~/anaconda3/lib/python3.8/site-packages/xgboost/core.py in inner_f(*args, **kwargs)
420 for k, arg in zip(sig.parameters, args):
421 kwargs[k] = arg
--> 422 return f(**kwargs)
423
424 return inner_f
~/anaconda3/lib/python3.8/site-packages/xgboost/sklearn.py in fit(self, X, y, sample_weight, base_margin, eval_set, eval_metric, early_stopping_rounds, verbose, xgb_model, sample_weight_eval_set, feature_weights, callbacks)
901 self.n_features_in_ = self._features_count
902
--> 903 train_dmatrix, evals = self._wrap_evaluation_matrices(
904 X, y, group=None, sample_weight=sample_weight, base_margin=base_margin,
905 feature_weights=feature_weights,
~/anaconda3/lib/python3.8/site-packages/xgboost/sklearn.py in _wrap_evaluation_matrices(self, X, y, group, sample_weight, base_margin, feature_weights, eval_set, sample_weight_eval_set, eval_group, label_transform)
263
264 y = label_transform(y)
--> 265 train_dmatrix = DMatrix(data=X, label=y, weight=sample_weight,
266 base_margin=base_margin,
267 missing=self.missing, nthread=self.n_jobs)
~/anaconda3/lib/python3.8/site-packages/xgboost/core.py in __init__(self, data, label, weight, base_margin, missing, silent, feature_names, feature_types, nthread, enable_categorical)
507 self.handle = handle
508
--> 509 self.set_info(label=label, weight=weight, base_margin=base_margin)
510
511 self.feature_names = feature_names
~/anaconda3/lib/python3.8/site-packages/xgboost/core.py in inner_f(*args, **kwargs)
420 for k, arg in zip(sig.parameters, args):
421 kwargs[k] = arg
--> 422 return f(**kwargs)
423
424 return inner_f
~/anaconda3/lib/python3.8/site-packages/xgboost/core.py in set_info(self, label, weight, base_margin, group, label_lower_bound, label_upper_bound, feature_names, feature_types, feature_weights)
528 '''Set meta info for DMatrix.'''
529 if label is not None:
--> 530 self.set_label(label)
531 if weight is not None:
532 self.set_weight(weight)
~/anaconda3/lib/python3.8/site-packages/xgboost/core.py in set_label(self, label)
657 """
658 from .data import dispatch_meta_backend
--> 659 dispatch_meta_backend(self, label, 'label', 'float')
660
661 def set_weight(self, weight):
~/anaconda3/lib/python3.8/site-packages/xgboost/data.py in dispatch_meta_backend(matrix, data, name, dtype)
674 data = data.values.astype('float')
675 assert len(data.shape) == 1 or data.shape[1] == 0 or data.shape[1] == 1
--> 676 _meta_from_numpy(data, name, dtype, handle)
677 return
678 if _is_dlpack(data):
~/anaconda3/lib/python3.8/site-packages/xgboost/data.py in _meta_from_numpy(data, field, dtype, handle)
598 ptr = interface['data'][0]
599 ptr = ctypes.c_void_p(ptr)
--> 600 _check_call(_LIB.XGDMatrixSetDenseInfo(
601 handle,
602 c_str(field),
~/anaconda3/lib/python3.8/ctypes/__init__.py in __getattr__(self, name)
392 if name.startswith('__') and name.endswith('__'):
393 raise AttributeError(name)
--> 394 func = self.__getitem__(name)
395 setattr(self, name, func)
396 return func
~/anaconda3/lib/python3.8/ctypes/__init__.py in __getitem__(self, name_or_ordinal)
397
398 def __getitem__(self, name_or_ordinal):
--> 399 func = self._FuncPtr((name_or_ordinal, self))
400 if not isinstance(name_or_ordinal, int):
401 func.__name__ = name_or_ordinal
AttributeError: /home/hp/anaconda3/lib/libxgboost.so: undefined symbol: XGDMatrixSetDenseInfo

Related

Colab IndexError: Target 255 is out of bounds

I'm trying to perform an image semantic segmentation (segment mining fields) using lightning-flash. My images are all RGB/uint8/512x512 and the masks are L/uint8/512x512.
When I run the code, I get an error when fitting.
My code is this one:
import torch
import flash
from flash.image import SemanticSegmentation, SemanticSegmentationData
import os
from google.colab import drive
import ssl
drive.mount("/content/drive")
DATA_DIR = '/content/drive/MyDrive/data/'
x_train_dir = os.path.join(DATA_DIR, 'train_images')
y_train_dir = os.path.join(DATA_DIR, 'train_masks')
x_valid_dir = os.path.join(DATA_DIR, 'val_images')
y_valid_dir = os.path.join(DATA_DIR, 'val_masks')
x_test_dir = os.path.join(DATA_DIR, 'test_images')
y_test_dir = os.path.join(DATA_DIR, 'test_masks')
datamodule = SemanticSegmentationData.from_folders(
train_folder=x_train_dir,
train_target_folder=y_train_dir,
val_folder=x_valid_dir,
val_target_folder=y_valid_dir,
test_folder=x_test_dir,
test_target_folder=y_test_dir,
transform_kwargs=dict(image_size=(256, 256)),
num_classes=1,
batch_size=16,
)
#avoid ssl error
ssl._create_default_https_context = ssl._create_unverified_context
model = SemanticSegmentation(
head="unetplusplus",
backbone="densenet169",
pretrained="imagenet",
num_classes=datamodule.num_classes
)
GPUS = torch.cuda.device_count()
if GPUS > 0:
trainer = flash.Trainer(max_epochs=2, gpus=torch.cuda.device_count())
else:
trainer = flash.Trainer(max_epochs=2)
trainer.finetune(model, datamodule=datamodule, strategy="freeze")
trainer.save_checkpoint("semantic_segmentation_model.pt")
When I run the code, I get this error:
IndexError Traceback (most recent call last)
<ipython-input-7-11e2ce087ca0> in <module>
6
7 #trainer.fit(model, datamodule=datamodule)
----> 8 trainer.finetune(model, datamodule=datamodule, strategy="freeze")
9 trainer.save_checkpoint("semantic_segmentation_model.pt")
19 frames
/usr/local/lib/python3.7/dist-packages/flash/core/trainer.py in finetune(self, model, train_dataloader, val_dataloaders, datamodule, strategy, train_bn)
162 """
163 self._resolve_callbacks(model, strategy, train_bn=train_bn)
--> 164 return super().fit(model, train_dataloader, val_dataloaders, datamodule)
165
166 def predict(
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
695 self.strategy.model = model
696 self._call_and_handle_interrupt(
--> 697 self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path
698 )
699
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _call_and_handle_interrupt(self, trainer_fn, *args, **kwargs)
648 return self.strategy.launcher.launch(trainer_fn, *args, trainer=self, **kwargs)
649 else:
--> 650 return trainer_fn(*args, **kwargs)
651 # TODO(awaelchli): Unify both exceptions below, where `KeyboardError` doesn't re-raise
652 except KeyboardInterrupt as exception:
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _fit_impl(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
735 ckpt_path, model_provided=True, model_connected=self.lightning_module is not None
736 )
--> 737 results = self._run(model, ckpt_path=self.ckpt_path)
738
739 assert self.state.stopped
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _run(self, model, ckpt_path)
1166 self._checkpoint_connector.resume_end()
1167
-> 1168 results = self._run_stage()
1169
1170 log.detail(f"{self.__class__.__name__}: trainer tearing down")
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _run_stage(self)
1252 if self.predicting:
1253 return self._run_predict()
-> 1254 return self._run_train()
1255
1256 def _pre_training_routine(self):
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _run_train(self)
1274
1275 with isolate_rng():
-> 1276 self._run_sanity_check()
1277
1278 # enable train mode
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _run_sanity_check(self)
1343 # run eval step
1344 with torch.no_grad():
-> 1345 val_loop.run()
1346
1347 self._call_callback_hooks("on_sanity_check_end")
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/loops/loop.py in run(self, *args, **kwargs)
198 try:
199 self.on_advance_start(*args, **kwargs)
--> 200 self.advance(*args, **kwargs)
201 self.on_advance_end()
202 self._restarting = False
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/loops/dataloader/evaluation_loop.py in advance(self, *args, **kwargs)
153 if self.num_dataloaders > 1:
154 kwargs["dataloader_idx"] = dataloader_idx
--> 155 dl_outputs = self.epoch_loop.run(self._data_fetcher, dl_max_batches, kwargs)
156
157 # store batch level output per dataloader
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/loops/loop.py in run(self, *args, **kwargs)
198 try:
199 self.on_advance_start(*args, **kwargs)
--> 200 self.advance(*args, **kwargs)
201 self.on_advance_end()
202 self._restarting = False
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/loops/epoch/evaluation_epoch_loop.py in advance(self, data_fetcher, dl_max_batches, kwargs)
141
142 # lightning module methods
--> 143 output = self._evaluation_step(**kwargs)
144 output = self._evaluation_step_end(output)
145
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/loops/epoch/evaluation_epoch_loop.py in _evaluation_step(self, **kwargs)
238 """
239 hook_name = "test_step" if self.trainer.testing else "validation_step"
--> 240 output = self.trainer._call_strategy_hook(hook_name, *kwargs.values())
241
242 return output
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/trainer/trainer.py in _call_strategy_hook(self, hook_name, *args, **kwargs)
1704
1705 with self.profiler.profile(f"[Strategy]{self.strategy.__class__.__name__}.{hook_name}"):
-> 1706 output = fn(*args, **kwargs)
1707
1708 # restore current_fx when nested context
/usr/local/lib/python3.7/dist-packages/pytorch_lightning/strategies/strategy.py in validation_step(self, *args, **kwargs)
368 with self.precision_plugin.val_step_context():
369 assert isinstance(self.model, ValidationStep)
--> 370 return self.model.validation_step(*args, **kwargs)
371
372 def test_step(self, *args: Any, **kwargs: Any) -> Optional[STEP_OUTPUT]:
/usr/local/lib/python3.7/dist-packages/flash/image/segmentation/model.py in validation_step(self, batch, batch_idx)
151 def validation_step(self, batch: Any, batch_idx: int) -> Any:
152 batch = (batch[DataKeys.INPUT], batch[DataKeys.TARGET])
--> 153 return super().validation_step(batch, batch_idx)
154
155 def test_step(self, batch: Any, batch_idx: int) -> Any:
/usr/local/lib/python3.7/dist-packages/flash/core/model.py in validation_step(self, batch, batch_idx)
423
424 def validation_step(self, batch: Any, batch_idx: int) -> None:
--> 425 output = self.step(batch, batch_idx, self.val_metrics)
426 log_kwargs = {"batch_size": output.get(OutputKeys.BATCH_SIZE, None)} if _PL_GREATER_EQUAL_1_5_0 else {}
427 self.log_dict(
/usr/local/lib/python3.7/dist-packages/flash/core/model.py in step(self, batch, batch_idx, metrics)
360 output = {OutputKeys.OUTPUT: y_hat}
361 y_hat = self.to_loss_format(output[OutputKeys.OUTPUT])
--> 362 losses = {name: l_fn(y_hat, y) for name, l_fn in self.loss_fn.items()}
363
364 y_hat = self.to_metrics_format(output[OutputKeys.OUTPUT])
/usr/local/lib/python3.7/dist-packages/flash/core/model.py in <dictcomp>(.0)
360 output = {OutputKeys.OUTPUT: y_hat}
361 y_hat = self.to_loss_format(output[OutputKeys.OUTPUT])
--> 362 losses = {name: l_fn(y_hat, y) for name, l_fn in self.loss_fn.items()}
363
364 y_hat = self.to_metrics_format(output[OutputKeys.OUTPUT])
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
3012 if size_average is not None or reduce is not None:
3013 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 3014 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
3015
3016
IndexError: Target 255 is out of bounds.
How can I solve this problem? I researched others issues on stackoverflow and they were all related to the number of classes. But in my case, I only want to segment mining fields.

'Line2D' object has no property 'ylabel' error with pd.plot()

I am trying to plot using df.plot from the pandas plotting library, and was using the following code:
df_mean.plot(kind='line', subplots=True, layout=(1,8), figsize=(40,8),
sharey=True, ylabel = "Percent Change", title="Average movement")
I thought it might have something to do with using np.transpose() since it would convert it into a numpy array, but after conversion back to a pd.DataFrame(), the error still persists.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-269-85f6c80ca026> in <module>
1 df_mean = pd.DataFrame(df_mean)
2
----> 3 df_mean.plot(kind='line', subplots=True, layout=(1,8), figsize=(40,8),
4 title="Average movement",
5 sharey=True, ylabel = "Percent Change")
~\anaconda3\lib\site-packages\pandas\plotting\_core.py in __call__(self, *args,
**kwargs)
845 keyword_args = ", ".join(
846 f"{name}={repr(value)}" for (name, default), value in
zip(arg_def, args)
--> 847 )
848 msg = (
849 "`Series.plot()` should not be called with positional "
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\__init__.py in plot(data,
kind, **kwargs)
59 kwargs["ax"] = getattr(ax, "left_ax", ax)
60 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 61 plot_obj.generate()
62 plot_obj.draw()
63 return plot_obj.result
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in generate(self)
261 else:
262 return self.data.shape[1]
--> 263
264 def draw(self):
265 self.plt.draw_if_interactive()
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in
_make_plot(self)
1075 self.data = self.data.fillna(value=0)
1076 self.x_compat = plot_params["x_compat"]
-> 1077 if "x_compat" in self.kwds:
1078 self.x_compat = bool(self.kwds.pop("x_compat"))
1079
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in _plot(cls, ax,
x, y, style, column_num, stacking_id, **kwds)
1102
1103 stacking_id = self._get_stacking_id()
-> 1104 is_errorbar = com.any_not_none(*self.errors.values())
1105
1106 colors = self._get_colors()
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\converter.py in
wrapper(*args, **kwargs)
64 with pandas_converters():
65 return func(*args, **kwargs)
---> 66
67 return wrapper
68
~\anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py in _plot(cls, ax,
x, y, style, is_errorbar, **kwds)
654
655 if is_errorbar:
--> 656 if "xerr" in kwds:
657 kwds["xerr"] = np.array(kwds.get("xerr"))
658 if "yerr" in kwds:
~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex,
scaley, data, *args, **kwargs)
1741
1742 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1743 lines = [*self._get_lines(*args, data=data, **kwargs)]
1744 for line in lines:
1745 self.add_line(line)
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, data,
*args, **kwargs)
271 this += args[0],
272 args = args[1:]
--> 273 yield from self._plot_args(this, kwargs)
274
275 def get_next_color(self):
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup,
kwargs)
416 if ncx > 1 and ncy > 1 and ncx != ncy:
417 raise ValueError(f"x has {ncx} columns but y has {ncy}
columns")
--> 418 return [func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
419 for j in range(max(ncx, ncy))]
420
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in <listcomp>(.0)
416 if ncx > 1 and ncy > 1 and ncx != ncy:
417 raise ValueError(f"x has {ncx} columns but y has {ncy}
columns")
--> 418 return [func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
419 for j in range(max(ncx, ncy))]
420
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _makeline(self, x, y,
kw, kwargs)
310 default_dict = self._getdefaults(set(), kw)
311 self._setdefaults(default_dict, kw)
--> 312 seg = mlines.Line2D(x, y, **kw)
313 return seg
314
~\anaconda3\lib\site-packages\matplotlib\lines.py in __init__(self, xdata, ydata,
linewidth, linestyle, color, marker, markersize, markeredgewidth, markeredgecolor,
markerfacecolor, markerfacecoloralt, fillstyle, antialiased, dash_capstyle,
solid_capstyle, dash_joinstyle, solid_joinstyle, pickradius, drawstyle, markevery,
**kwargs)
388 # update kwargs before updating data to give the caller a
389 # chance to init axes (and hence unit support)
--> 390 self.update(kwargs)
391 self.pickradius = pickradius
392 self.ind_offset = 0
~\anaconda3\lib\site-packages\matplotlib\artist.py in update(self, props)
994 func = getattr(self, f"set_{k}", None)
995 if not callable(func):
--> 996 raise AttributeError(f"{type(self).__name__!r}
object "
997 f"has no property {k!r}")
998 ret.append(func(v))
AttributeError: 'Line2D' object has no property 'ylabel'
I was able to run this code fine on my mac, but when I transferred it over to my desktop, I get this error, and I'm not sure why. I thought it could be a version problem, but I updated pandas and it didn't fix anything.
Anybody have an idea what could be causing something like this?
You can try this trick:
If the ylabel parameter is the problem, remove it and set it directly to ax.
ax = df_mean.plot(kind='line', subplots=True, layout=(1,8), figsize=(40,8),
sharey=True, title="Average movement")
ax.set_ylabel('Percent Change')
plt.show()

M1 MacBook Apple ML compute Tensorflow2.4 compatibility issue with Numpy

I am running the new apple native tensorflow package 2.4, and ran into a problem I did not have before. This jupyter notebook code works in old intel based environment where an older tensorflow version was used. But with M1 apple MLcomputer TensorFlow2.4 it is not compatible
with Numpy 1.20 or 1.18(I downgraded numpy to try). The error log:
NotImplementedError: Cannot convert a symbolic Tensor (lstm_1/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-20-73358e637fe3> in <module>
4 model = Sequential()
5 model.add(Embedding(vocab_size+1, W2V_SIZE, weights=[embedding_matrix], input_length=MAX_SEQUENCE_LENGTH, trainable=False))
----> 6 model.add(LSTM(500, dropout=0.2, recurrent_dropout=0.2))
7 model.add(Dense(units = 10000, kernel_initializer = 'glorot_uniform', activation = 'relu'))
8 model.add(Dropout(0.35))
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
515 self._self_setattr_tracking = False # pylint: disable=protected-access
516 try:
--> 517 result = method(self, *args, **kwargs)
518 finally:
519 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
221 # If the model is being built continuously on top of an input layer:
222 # refresh its output.
--> 223 output_tensor = layer(self.outputs[0])
224 if len(nest.flatten(output_tensor)) != 1:
225 raise ValueError(SINGLE_LAYER_OUTPUT_ERROR_MSG)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
658
659 if initial_state is None and constants is None:
--> 660 return super(RNN, self).__call__(inputs, **kwargs)
661
662 # If any of `initial_state` or `constants` are specified and are Keras
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
944 # >> model = tf.keras.Model(inputs, outputs)
945 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 946 return self._functional_construction_call(inputs, args, kwargs,
947 input_list)
948
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1083 layer=self, inputs=inputs, build_graph=True, training=training_value):
1084 # Check input assumptions set after layer building, e.g. input shape.
-> 1085 outputs = self._keras_tensor_symbolic_call(
1086 inputs, input_masks, args, kwargs)
1087
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
815 return nest.map_structure(keras_tensor.KerasTensor, output_signature)
816 else:
--> 817 return self._infer_output_signature(inputs, args, kwargs, input_masks)
818
819 def _infer_output_signature(self, inputs, args, kwargs, input_masks):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
856 # TODO(kaftan): do we maybe_build here, or have we already done it?
857 self._maybe_build(inputs)
--> 858 outputs = call_fn(inputs, *args, **kwargs)
859
860 self._handle_activity_regularization(inputs, outputs)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent_v2.py in call(self, inputs, mask, training, initial_state)
1161 # LSTM does not support constants. Ignore it during process.
1162 orig_initial_state = initial_state
-> 1163 inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
1164
1165 if isinstance(mask, list):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in _process_inputs(self, inputs, initial_state, constants)
857 initial_state = self.states
858 elif initial_state is None:
--> 859 initial_state = self.get_initial_state(inputs)
860
861 if len(initial_state) != len(self.states):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in get_initial_state(self, inputs)
640 dtype = inputs.dtype
641 if get_initial_state_fn:
--> 642 init_state = get_initial_state_fn(
643 inputs=None, batch_size=batch_size, dtype=dtype)
644 else:
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in get_initial_state(self, inputs, batch_size, dtype)
2504
2505 def get_initial_state(self, inputs=None, batch_size=None, dtype=None):
-> 2506 return list(_generate_zero_filled_state_for_cell(
2507 self, inputs, batch_size, dtype))
2508
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in _generate_zero_filled_state_for_cell(cell, inputs, batch_size, dtype)
2985 batch_size = array_ops.shape(inputs)[0]
2986 dtype = inputs.dtype
-> 2987 return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
2988
2989
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in _generate_zero_filled_state(batch_size_tensor, state_size, dtype)
3001
3002 if nest.is_nested(state_size):
-> 3003 return nest.map_structure(create_zeros, state_size)
3004 else:
3005 return create_zeros(state_size)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/util/nest.py in map_structure(func, *structure, **kwargs)
657
658 return pack_sequence_as(
--> 659 structure[0], [func(*x) for x in entries],
660 expand_composites=expand_composites)
661
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/util/nest.py in <listcomp>(.0)
657
658 return pack_sequence_as(
--> 659 structure[0], [func(*x) for x in entries],
660 expand_composites=expand_composites)
661
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in create_zeros(unnested_state_size)
2998 flat_dims = tensor_shape.TensorShape(unnested_state_size).as_list()
2999 init_state_size = [batch_size_tensor] + flat_dims
-> 3000 return array_ops.zeros(init_state_size, dtype=dtype)
3001
3002 if nest.is_nested(state_size):
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 """Call target, and fall back on dispatchers if there is a TypeError."""
200 try:
--> 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py in wrapped(*args, **kwargs)
2817
2818 def wrapped(*args, **kwargs):
-> 2819 tensor = fun(*args, **kwargs)
2820 tensor._is_zeros_tensor = True
2821 return tensor
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py in zeros(shape, dtype, name)
2866 # Create a constant if it won't be very big. Otherwise create a fill
2867 # op to prevent serialized GraphDefs from becoming too large.
-> 2868 output = _constant_if_small(zero, shape, dtype, name)
2869 if output is not None:
2870 return output
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py in _constant_if_small(value, shape, dtype, name)
2802 def _constant_if_small(value, shape, dtype, name):
2803 try:
-> 2804 if np.prod(shape) < 1000:
2805 return constant(value, shape=shape, dtype=dtype, name=name)
2806 except TypeError:
<__array_function__ internals> in prod(*args, **kwargs)
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/numpy/core/fromnumeric.py in prod(a, axis, dtype, out, keepdims, initial, where)
3028 10
3029 """
-> 3030 return _wrapreduction(a, np.multiply, 'prod', axis, dtype, out,
3031 keepdims=keepdims, initial=initial, where=where)
3032
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
85 return reduction(axis=axis, out=out, **passkwargs)
86
---> 87 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
88
89
~/miniforge3/envs/tf2.4/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in __array__(self)
850
851 def __array__(self):
--> 852 raise NotImplementedError(
853 "Cannot convert a symbolic Tensor ({}) to a numpy array."
854 " This error may indicate that you're trying to pass a Tensor to"
NotImplementedError: Cannot convert a symbolic Tensor (lstm_1/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

keras.backend is not defined for variational autoencoder model

I created a Variational Autoencoder model. To do the sampling, I created the following method:
from keras import backend as k
def sampling(args):
z_mean , z_log_var=args
batch=k.shape(z_mean)[0]
dim=k.int_shape(z_mean)[1]
epsilon=k.random_normal(shape=(batch,dim))
return z_mean + k.exp(0.5 * z_log_var) * epsilon
this is the model architecture:
def create_variationalModel(original_dim):
input_shape=(original_dim,)
intermidiate_dim=58
batch_size=10
latent_dim=3
epochs=100
inputs=Input(shape=input_shape,name="encoder_input")
x= Dense(units=original_dim,activation="tanh")(inputs)
x= Dense(units=int(original_dim/2),activation="tanh")(x)
x1= Dense(units=int(original_dim/4),activation="tanh")(x)
x2= Dense(units=int(original_dim/8),activation="tanh")(x1)
x3= Dense(units=10,activation="tanh")(x2)
z_mean=Dense(latent_dim,name="z_mean")(x3)
z_log_var=Dense(latent_dim,name="z_log_var")(x3)
z=Lambda(sampling,output_shape=(latent_dim,),name="z")([z_mean,z_log_var])
encoder=Model(inputs,[z_mean,z_log_var,z],name="encoder")
encoder.summary()
latent_inputs=Input(shape=(latent_dim,),name="z_sampling")
x= Dense(units=10,activation="tanh")(latent_inputs)
x1=Dense(units=int(original_dim/8),activation="tanh")(x)
x2=Dense(units=int(original_dim/4),activation="tanh")(x1)
x3=Dense(units=int(original_dim/2),activation="tanh")(x2)
x3=Dense(units=original_dim,activation="tanh")(x3)
outputs=Dense(units=original_dim,activation="sigmoid")(x3)
decoder=Model(latent_inputs,outputs,name="decoder")
decoder.summary()
outputs=decoder(encoder(inputs)[2])
vae = Model(inputs,outputs,name="vae_mlp")
reconstruction_loss=mse(inputs,outputs)
reconstruction_loss*=original_dim
kl_loss = 1 + z_log_var -k.square(z_mean) - k.exp(z_log_var)
kl_loss=k.sum(kl_loss,axis=-1)
kl_loss*=-0.5
vae_loss=k.mean(reconstruction_loss+kl_loss)
vae.add_loss(vae_loss)
plot_model(vae,to_file='vae.png',show_shapes=True)
vae.compile(optimizer=RMSprop(),loss="mean_squared_error",metrics=["mae"])
return vae
the after training the model and test it, I decide to save it like this:
vae.save("./models/vae.h5")
but when I tried to load the model like this:
model = load_model("./models/vae.h5")
I have this issue:
--------------------------------------------------------------------------- NameError Traceback (most recent call
last) in
1 #load model
----> 2 model = load_model("./models/vae.h5")
3 # summarize model.
4 model.summary()
5 with open("./models/LabelEncoders_dic.pickle","rb") as f:
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/saving.py
in load_wrapper(*args, **kwargs)
490 os.remove(tmp_filepath)
491 return res
--> 492 return load_function(*args, **kwargs)
493
494 return load_wrapper
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/saving.py
in load_model(filepath, custom_objects, compile)
582 if H5Dict.is_supported_type(filepath):
583 with H5Dict(filepath, mode='r') as h5dict:
--> 584 model = _deserialize_model(h5dict, custom_objects, compile)
585 elif hasattr(filepath, 'write') and callable(filepath.write):
586 def load_function(h5file):
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/saving.py
in _deserialize_model(h5dict, custom_objects, compile)
272 raise ValueError('No model found in config.')
273 model_config = json.loads(model_config.decode('utf-8'))
--> 274 model = model_from_config(model_config, custom_objects=custom_objects)
275 model_weights_group = h5dict['model_weights']
276
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/saving.py
in model_from_config(config, custom_objects)
625 'Sequential.from_config(config)?')
626 from ..layers import deserialize
--> 627 return deserialize(config, custom_objects=custom_objects)
628
629
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/layers/init.py
in deserialize(config, custom_objects)
163 globs['Model'] = models.Model
164 globs['Sequential'] = models.Sequential
--> 165 return deserialize_keras_object(config,
166 module_objects=globs,
167 custom_objects=custom_objects,
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/utils/generic_utils.py
in deserialize_keras_object(identifier, module_objects,
custom_objects, printable_module_name)
142 custom_objects = custom_objects or {}
143 if has_arg(cls.from_config, 'custom_objects'):
--> 144 return cls.from_config(
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/network.py
in from_config(cls, config, custom_objects) 1054 # First,
we create all layers and enqueue nodes to be processed 1055
for layer_data in config['layers']:
-> 1056 process_layer(layer_data) 1057 1058 # Then we process nodes in order of layer depth.
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/network.py
in process_layer(layer_data) 1039 from ..layers import
deserialize as deserialize_layer 1040
-> 1041 layer = deserialize_layer(layer_data, 1042 custom_objects=custom_objects) 1043
created_layers[layer_name] = layer
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/layers/init.py
in deserialize(config, custom_objects)
163 globs['Model'] = models.Model
164 globs['Sequential'] = models.Sequential
--> 165 return deserialize_keras_object(config,
166 module_objects=globs,
167 custom_objects=custom_objects,
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/utils/generic_utils.py
in deserialize_keras_object(identifier, module_objects,
custom_objects, printable_module_name)
142 custom_objects = custom_objects or {}
143 if has_arg(cls.from_config, 'custom_objects'):
--> 144 return cls.from_config(
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/network.py
in from_config(cls, config, custom_objects) 1073
node_data = node_data_list[node_index] 1074
try:
-> 1075 process_node(layer, node_data) 1076 1077 # If the node does not have all
inbound layers
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/network.py
in process_node(layer, node_data) 1023 # and building
the layer if needed. 1024 if input_tensors:
-> 1025 layer(unpack_singleton(input_tensors), **kwargs) 1026 1027 def process_layer(layer_data):
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/backend/tensorflow_backend.py
in symbolic_fn_wrapper(*args, **kwargs)
73 if _SYMBOLIC_SCOPE.value:
74 with get_graph().as_default():
---> 75 return func(*args, **kwargs)
76 else:
77 return func(*args, **kwargs)
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/engine/base_layer.py
in call(self, inputs, **kwargs)
487 # Actually call the layer,
488 # collecting output(s), mask(s), and shape(s).
--> 489 output = self.call(inputs, **kwargs)
490 output_mask = self.compute_mask(inputs, previous_mask)
491
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/layers/core.py
in call(self, inputs, mask)
714 else:
715 self._input_dtypes = K.dtype(inputs)
--> 716 return self.function(inputs, **arguments)
717
718 def compute_mask(self, inputs, mask=None):
~/anaconda3/envs/myenv/lib/python3.8/site-packages/keras/layers/core.py
in sampling(args)
NameError: name 'k' is not defined
K comes from from keras import backend as k. even y adding this importation, I have the same error. Can anyone know how to fix this ?

folium heatmap with `Object of type 'int64' is not JSON serializable` error

I got an error as "Object of type 'int64' is not JSON serializable" when I tried to generate heatmap from folium.
I am running my jupyter notebook in anaconda using python 3.6, and the version of folium is '0.9.1'.
df_2y_cons_LatLo.dtypes: Latitude float64;
Longitude float64;
Descriptor int64.
def generateBaseMap(default_location=[40.704652, -73.923688], default_zoom_start=11):
base_map = folium.Map(location=default_location, control_scale=True, zoom_start=default_zoom_start)
return base_map
base_map = generateBaseMap()
hm = HeatMap(list(zip(df_2y_cons_LatLo.Latitude.values,df_2y_cons_LatLo.Longitude.values,df_2y_cons_LatLo.Descriptor.values)))
base_map.add_child(hm)
I expected to have a heatmap showing. But, after I run the above code, it gave me the error mentioned above.
TypeError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
343 method = get_real_method(obj, self.print_method)
344 if method is not None:
--> 345 return method()
346 return None
347 else:
~\Anaconda3\lib\site-packages\folium\folium.py in _repr_html_(self, **kwargs)
291 self._parent = None
292 else:
--> 293 out = self._parent._repr_html_(**kwargs)
294 return out
295
~\Anaconda3\lib\site-packages\branca\element.py in _repr_html_(self, **kwargs)
326
327 """
--> 328 html = self.render(**kwargs)
329 html = "data:text/html;charset=utf-8;base64," + base64.b64encode(html.encode('utf8')).decode('utf8') # noqa
330
~\Anaconda3\lib\site-packages\branca\element.py in render(self, **kwargs)
319 """Renders the HTML representation of the element."""
320 for name, child in self._children.items():
--> 321 child.render(**kwargs)
322 return self._template.render(this=self, kwargs=kwargs)
323
~\Anaconda3\lib\site-packages\folium\folium.py in render(self, **kwargs)
368 '</style>'), name='map_style')
369
--> 370 super(Map, self).render(**kwargs)
371
372 def fit_bounds(self, bounds, padding_top_left=None,
~\Anaconda3\lib\site-packages\branca\element.py in render(self, **kwargs)
631
632 for name, element in self._children.items():
--> 633 element.render(**kwargs)
~\Anaconda3\lib\site-packages\folium\plugins\heat_map.py in render(self, **kwargs)
79
80 def render(self, **kwargs):
---> 81 super(HeatMap, self).render(**kwargs)
82
83 figure = self.get_root()
~\Anaconda3\lib\site-packages\branca\element.py in render(self, **kwargs)
627 script = self._template.module.__dict__.get('script', None)
628 if script is not None:
--> 629 figure.script.add_child(Element(script(self, kwargs)),
630 name=self.get_name())
631
~\Anaconda3\lib\site-packages\jinja2\runtime.py in __call__(self, *args, **kwargs)
573 (self.name, len(self.arguments)))
574
--> 575 return self._invoke(arguments, autoescape)
576
577 def _invoke(self, arguments, autoescape):
~\Anaconda3\lib\site-packages\jinja2\asyncsupport.py in _invoke(self, arguments, autoescape)
108 def _invoke(self, arguments, autoescape):
109 if not self._environment.is_async:
--> 110 return original_invoke(self, arguments, autoescape)
111 return async_invoke(self, arguments, autoescape)
112 return update_wrapper(_invoke, original_invoke)
~\Anaconda3\lib\site-packages\jinja2\runtime.py in _invoke(self, arguments, autoescape)
577 def _invoke(self, arguments, autoescape):
578 """This method is being swapped out by the async implementation."""
--> 579 rv = self._func(*arguments)
580 if autoescape:
581 rv = Markup(rv)
<template> in macro(l_1_this, l_1_kwargs)
~\Anaconda3\lib\site-packages\jinja2\filters.py in do_tojson(eval_ctx, value, indent)
1076 options = dict(options)
1077 options['indent'] = indent
-> 1078 return htmlsafe_json_dumps(value, dumper=dumper, **options)
1079
1080
~\Anaconda3\lib\site-packages\jinja2\utils.py in htmlsafe_json_dumps(obj, dumper, **kwargs)
563 if dumper is None:
564 dumper = json.dumps
--> 565 rv = dumper(obj, **kwargs) \
566 .replace(u'<', u'\\u003c') \
567 .replace(u'>', u'\\u003e') \
~\Anaconda3\lib\json\__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
236 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
237 separators=separators, default=default, sort_keys=sort_keys,
--> 238 **kw).encode(obj)
239
240
~\Anaconda3\lib\json\encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~\Anaconda3\lib\json\encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
~\Anaconda3\lib\json\encoder.py in default(self, o)
178 """
179 raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180 o.__class__.__name__)
181
182 def encode(self, o):
TypeError: Object of type 'int64' is not JSON serializable

Resources