Cannot run pool.amap() twice in pathos.multiprocessing - multiprocessing

I want to parallelize simulation of multiple agents. Because I want my results to be instances of a class, to avoid issues with serialization I use pathos.multiprocessing instead of multiprocessing.
I do it like this:
import pathos.multiprocessing as mp
sim_agent(T): #simulate single agent for T periods
ag = agent()
for t in range(T):
ag.step()
return ag
def simulate_parallel(N, T):
if __name__ == '__main__':
pool = mp.ProcessPool()
results = pool.amap(sim_agent, [T]*N)
agents = results.get()
return agents
I can run simulate_parallel once. When I do it again, I get an error:
--> 142 agents = results.get()
~/anaconda3/anaconda3/lib/python3.7/site-packages/multiprocess/pool.py in get(self, timeout)
655 return self._value
656 else:
--> 657 raise self._value
658
659 def _set(self, i, obj):
~/anaconda3/anaconda3/lib/python3.7/site-packages/multiprocess/pool.py in _handle_tasks(taskqueue, put, outqueue, pool, cache)
429 break
430 try:
--> 431 put(task)
432 except Exception as e:
433 job, idx = task[:2]
~/anaconda3/anaconda3/lib/python3.7/site-packages/multiprocess/connection.py in send(self, obj)
207 self._check_closed()
208 self._check_writable()
--> 209 self._send_bytes(_ForkingPickler.dumps(obj))
210
211 def recv_bytes(self, maxlength=None):
~/anaconda3/anaconda3/lib/python3.7/site-packages/multiprocess/connection.py in _send_bytes(self, buf)
394 n = len(buf)
395 # For wire compatibility with 3.2 and lower
--> 396 header = struct.pack("!i", n)
397 if n > 16384:
398 # The payload is large so Nagle's algorithm won't be triggered
error: 'i' format requires -2147483648 <= number <= 2147483647
I also get the same error when I try to use pool.amap() in other functions. Even when I make N and T small, it is still the same behavior: I can run simulate_parallel once and cannot do it for the second time. Why could that be? Thanks!

Related

Cannot compile DQN agent: TypeError: ('Keyword argument not understood:', 'units')

I have this model:
poss_in = layers.Input((1,))
poss_lr = layers.Dense(8, activation='relu')(poss_in)
hist_in = layers.Input((100,))
hist_lr = layers.Reshape((100, 1))(hist_in)
hist_lr = layers.LSTM(32)(hist_lr)
hist_lr = layers.Dense(32, activation='relu')(hist_lr)
sent_in = layers.Input((10,))
sent_lr = layers.Reshape((10, 1))(sent_in)
sent_lr = layers.Conv1D(4, 3)(sent_lr)
sent_lr = layers.GRU(4)(sent_lr)
root_lr = layers.concatenate([poss_lr, hist_lr, sent_lr])
root_lr = layers.Reshape((44, 1))(root_lr)
root_lr = Attention(16)(root_lr)
root_lr = layers.Dense(16)(root_lr)
root_lr = layers.Dense(1)(root_lr)
model = Model([poss_in, hist_in, sent_in], root_lr)
and I'm trying to create a DQN agent with:
dqn = agents.DQNAgent(
model=model,
memory=memory.SequentialMemory(limit=50000, window_length=1),
policy=policy.BoltzmannQPolicy(),
nb_actions=1,
nb_steps_warmup=64,
target_model_update=1e-2
)
dqn.compile('Adam', metrics=['mae'])
but I receive this error:
/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/adam.py:105: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.
super(Adam, self).__init__(name, **kwargs)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-3d71fb800af2> in <module>
7 target_model_update=1e-2
8 )
----> 9 dqn.compile(opt.Adam(lr=1e-3), metrics=['mae'])
17 frames
/usr/local/lib/python3.7/dist-packages/rl/agents/dqn.py in compile(self, optimizer, metrics)
165
166 # We never train the target model, hence we can set the optimizer and loss arbitrarily.
--> 167 self.target_model = clone_model(self.model, self.custom_model_objects)
168 self.target_model.compile(optimizer='sgd', loss='mse')
169 self.model.compile(optimizer='sgd', loss='mse')
/usr/local/lib/python3.7/dist-packages/rl/util.py in clone_model(model, custom_objects)
13 'config': model.get_config(),
14 }
---> 15 clone = model_from_config(config, custom_objects=custom_objects)
16 clone.set_weights(model.get_weights())
17 return clone
/usr/local/lib/python3.7/dist-packages/keras/saving/model_config.py in model_from_config(config, custom_objects)
50 '`Sequential.from_config(config)`?')
51 from keras.layers import deserialize # pylint: disable=g-import-not-at-top
---> 52 return deserialize(config, custom_objects=custom_objects)
53
54
/usr/local/lib/python3.7/dist-packages/keras/layers/serialization.py in deserialize(config, custom_objects)
209 module_objects=LOCAL.ALL_OBJECTS,
210 custom_objects=custom_objects,
--> 211 printable_module_name='layer')
212
213
/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
681 custom_objects=dict(
682 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 683 list(custom_objects.items())))
684 else:
685 with CustomObjectScope(custom_objects):
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py in from_config(cls, config, custom_objects)
707 'name', 'layers', 'input_layers', 'output_layers']):
708 input_tensors, output_tensors, created_layers = reconstruct_from_config(
--> 709 config, custom_objects)
710 model = cls(
711 inputs=input_tensors,
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py in reconstruct_from_config(config, custom_objects, created_layers)
1324 # First, we create all layers and enqueue nodes to be processed
1325 for layer_data in config['layers']:
-> 1326 process_layer(layer_data)
1327 # Then we process nodes in order of layer depth.
1328 # Nodes that cannot yet be processed (if the inbound node
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py in process_layer(layer_data)
1306 from keras.layers import deserialize as deserialize_layer # pylint: disable=g-import-not-at-top
1307
-> 1308 layer = deserialize_layer(layer_data, custom_objects=custom_objects)
1309 created_layers[layer_name] = layer
1310
/usr/local/lib/python3.7/dist-packages/keras/layers/serialization.py in deserialize(config, custom_objects)
209 module_objects=LOCAL.ALL_OBJECTS,
210 custom_objects=custom_objects,
--> 211 printable_module_name='layer')
212
213
/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
684 else:
685 with CustomObjectScope(custom_objects):
--> 686 deserialized_obj = cls.from_config(cls_config)
687 else:
688 # Then `cls` may be a function returning a class.
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer_v1.py in from_config(cls, config)
515 A layer instance.
516 """
--> 517 return cls(**config)
518
519 def compute_output_shape(self, input_shape):
/usr/local/lib/python3.7/dist-packages/keras/layers/dense_attention.py in __init__(self, use_scale, **kwargs)
321
322 def __init__(self, use_scale=False, **kwargs):
--> 323 super(Attention, self).__init__(**kwargs)
324 self.use_scale = use_scale
325
/usr/local/lib/python3.7/dist-packages/keras/layers/dense_attention.py in __init__(self, causal, dropout, **kwargs)
70
71 def __init__(self, causal=False, dropout=0.0, **kwargs):
---> 72 super(BaseDenseAttention, self).__init__(**kwargs)
73 self.causal = causal
74 self.dropout = dropout
/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
627 self._self_setattr_tracking = False # pylint: disable=protected-access
628 try:
--> 629 result = method(self, *args, **kwargs)
630 finally:
631 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py in __init__(self, seed, force_generator, **kwargs)
3436 **kwargs: other keyword arguments that will be passed to the parent class
3437 """
-> 3438 super().__init__(**kwargs)
3439 self._random_generator = backend.RandomGenerator(
3440 seed, force_generator=force_generator)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
627 self._self_setattr_tracking = False # pylint: disable=protected-access
628 try:
--> 629 result = method(self, *args, **kwargs)
630 finally:
631 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer_v1.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
138 }
139 # Validate optional keyword arguments.
--> 140 generic_utils.validate_kwargs(kwargs, allowed_kwargs)
141
142 # Mutable properties
/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
1172 for kwarg in kwargs:
1173 if kwarg not in allowed_kwargs:
-> 1174 raise TypeError(error_message, kwarg)
1175
1176
TypeError: ('Keyword argument not understood:', 'units')
I have tryied to replace the DQN with SARSA and DDPG agents but they all generated the same error.
I looked up the problem in internet for a while and I've asked on r/tensorflow but I haven't resolved anything yet.
For additional information, I'm using Google Colab.
Thanks for every reply!
UPDATE:
I tryied to simplify the model in order to check if the problem was in a layer, so I created this model:
poss_in = layers.Input((1,))
poss_lr = layers.Dense(1)(poss_in)
hist_in = layers.Input((100,))
hist_lr = layers.Dense(1)(hist_in)
sent_in = layers.Input((10,))
sent_lr = layers.Dense(1)(sent_in)
root_lr = layers.concatenate([poss_lr, hist_lr, sent_lr])
root_lr = layers.Dense(1)(root_lr)
model = Model([poss_in, hist_in, sent_in], root_lr)
Using this model the DQN agent was compiled with no errors.

How to get the shap values for the masked language modeling task using transformer?

I am trying to get the shap values for the masked language modeling task using transformer. I get the error KeyError: 'label' for the code where I input a single data sample to get the explanation. My complete code and error trace are as follows:
import transformers
import shap
from transformers import RobertaTokenizer, RobertaForMaskedLM, pipeline
import torch
model = RobertaForMaskedLM.from_pretrained('microsoft/codebert-base-mlm')
tokenizer = RobertaTokenizer.from_pretrained('microsoft/codebert-base-mlm')
code_example = "if (x <mask> 10)"
fill_mask = pipeline('fill-mask', model=model, tokenizer=tokenizer)
explainer = shap.Explainer(fill_mask)
shap_values = explainer(['x {tokenizer.mask_token} 10'])
Following is the error trace
KeyError Traceback (most recent call last)
[<ipython-input-12-bb3832d1772d>](https://localhost:8080/#) in <module>
6 # explain the model on two sample inputs
7 explainer = shap.Explainer(fill_mask)
----> 8 shap_values = explainer(['x {tokenizer.mask_token} 10'])
9 print(shap_values)
10 # visualize the first prediction's explanation for the POSITIVE output class
5 frames
[/usr/local/lib/python3.7/dist-packages/shap/explainers/_partition.py](https://localhost:8080/#) in __call__(self, max_evals, fixed_context, main_effects, error_bounds, batch_size, outputs, silent, *args)
136 return super().__call__(
137 *args, max_evals=max_evals, fixed_context=fixed_context, main_effects=main_effects, error_bounds=error_bounds, batch_size=batch_size,
--> 138 outputs=outputs, silent=silent
139 )
140
[/usr/local/lib/python3.7/dist-packages/shap/explainers/_explainer.py](https://localhost:8080/#) in __call__(self, max_evals, main_effects, error_bounds, batch_size, outputs, silent, *args, **kwargs)
266 row_result = self.explain_row(
267 *row_args, max_evals=max_evals, main_effects=main_effects, error_bounds=error_bounds,
--> 268 batch_size=batch_size, outputs=outputs, silent=silent, **kwargs
269 )
270 values.append(row_result.get("values", None))
[/usr/local/lib/python3.7/dist-packages/shap/explainers/_partition.py](https://localhost:8080/#) in explain_row(self, max_evals, main_effects, error_bounds, batch_size, outputs, silent, fixed_context, *row_args)
159 # if not fixed background or no base value assigned then compute base value for a row
160 if self._curr_base_value is None or not getattr(self.masker, "fixed_background", False):
--> 161 self._curr_base_value = fm(m00.reshape(1, -1), zero_index=0)[0] # the zero index param tells the masked model what the baseline is
162 f11 = fm(~m00.reshape(1, -1))[0]
163
[/usr/local/lib/python3.7/dist-packages/shap/utils/_masked_model.py](https://localhost:8080/#) in __call__(self, masks, zero_index, batch_size)
65
66 else:
---> 67 return self._full_masking_call(masks, batch_size=batch_size)
68
69 def _full_masking_call(self, masks, zero_index=None, batch_size=None):
[/usr/local/lib/python3.7/dist-packages/shap/utils/_masked_model.py](https://localhost:8080/#) in _full_masking_call(self, masks, zero_index, batch_size)
142
143 joined_masked_inputs = tuple([np.concatenate(v) for v in all_masked_inputs])
--> 144 outputs = self.model(*joined_masked_inputs)
145 _assert_output_input_match(joined_masked_inputs, outputs)
146 all_outputs.append(outputs)
[/usr/local/lib/python3.7/dist-packages/shap/models/_transformers_pipeline.py](https://localhost:8080/#) in __call__(self, strings)
33 val = [val]
34 for obj in val:
---> 35 output[i, self.label2id[obj["label"]]] = sp.special.logit(obj["score"]) if self.rescale_to_logits else obj["score"]
36 return output
KeyError: 'label'

Problem using spacy tokenizer for count vectorizer

I'm trying to do sentiment analysis on Amazon product reviews using the Spacy module for preprocessing the text data. The code I'm using is exactly this. I modified the dataset that I'm using according to what's shown in the link. I'm getting the error:
TypeError Traceback (most recent call last)
<ipython-input-139-bcbf2d3c9cce> in <module>
4 ('classifier', classifier)])
5 # Fit our data
----> 6 pipe_countvect.fit(X_train,y_train)
7 # Predicting with a test dataset
8 sample_prediction = pipe_countvect.predict(X_test)
~\.conda\envs\py36\lib\site-packages\sklearn\pipeline.py in fit(self, X, y, **fit_params)
328 """
329 fit_params_steps = self._check_fit_params(**fit_params)
--> 330 Xt = self._fit(X, y, **fit_params_steps)
331 with _print_elapsed_time('Pipeline',
332 self._log_message(len(self.steps) - 1)):
~\.conda\envs\py36\lib\site-packages\sklearn\pipeline.py in _fit(self, X, y, **fit_params_steps)
294 message_clsname='Pipeline',
295 message=self._log_message(step_idx),
--> 296 **fit_params_steps[name])
297 # Replace the transformer of the step with the fitted
298 # transformer. This is necessary when loading the transformer
~\.conda\envs\py36\lib\site-packages\joblib\memory.py in __call__(self, *args, **kwargs)
350
351 def __call__(self, *args, **kwargs):
--> 352 return self.func(*args, **kwargs)
353
354 def call_and_shelve(self, *args, **kwargs):
~\.conda\envs\py36\lib\site-packages\sklearn\pipeline.py in _fit_transform_one(transformer, X, y, weight, message_clsname, message, **fit_params)
738 with _print_elapsed_time(message_clsname, message):
739 if hasattr(transformer, 'fit_transform'):
--> 740 res = transformer.fit_transform(X, y, **fit_params)
741 else:
742 res = transformer.fit(X, y, **fit_params).transform(X)
~\.conda\envs\py36\lib\site-packages\sklearn\feature_extraction\text.py in fit_transform(self, raw_documents, y)
1197
1198 vocabulary, X = self._count_vocab(raw_documents,
-> 1199 self.fixed_vocabulary_)
1200
1201 if self.binary:
~\.conda\envs\py36\lib\site-packages\sklearn\feature_extraction\text.py in _count_vocab(self, raw_documents, fixed_vocab)
1108 for doc in raw_documents:
1109 feature_counter = {}
-> 1110 for feature in analyze(doc):
1111 try:
1112 feature_idx = vocabulary[feature]
~\.conda\envs\py36\lib\site-packages\sklearn\feature_extraction\text.py in _analyze(doc, analyzer, tokenizer, ngrams, preprocessor, decoder, stop_words)
104 doc = preprocessor(doc)
105 if tokenizer is not None:
--> 106 doc = tokenizer(doc)
107 if ngrams is not None:
108 if stop_words is not None:
TypeError: 'str' object is not callable
I'm not sure what's causing this error and how to get rid of it. I'm pretty sure the count vectorizer produces a sparse matrix and not a string one. One thing that I've considered is that I'm using the spacy tokenizer, which was used in the link as vectorizer = CountVectorizer(tokenizer = spacy_tokenizer, ngram_range=(1,1)) but when I ran the program it was saying that spacy_tokenizer was undefined. So I used vectorizer = CountVectorizer(tokenizer = 'spacy', ngram_range=(1,1)) instead. But if I remove this then I don't know how to use the spacy tokenizer, and either way I am not certain that this was indeed the cause of the problem. Please help me out!
The error comes at this line:
doc = tokenizer(doc)
Since it says 'str' is not callable and the only thing being called here is the tokenizer object, it looks like your tokenizer is a string for some reason.
Based on the code you linked it looks like the spacy_tokenizer object is being configured incorrectly. But that variable isn't defined anywhere in the code despite being passed as an option, so the code you linked to looks like it can't possibly run.
It would help if you could make a minimal example that you could actually paste in the question here.

KeyError: 'Entity c does not exist in dfs'

when i try to run this code,
ftr_mtrx_custmr, features_defs = ft.dfs(entities=entities,
relationships=relationship,
target_entity="transactions")
i get such error,
490 featuretools.entityset - WARNING index session_id not found in dataframe, creating new integer column
KeyError Traceback (most recent call last)
<ipython-input-82-d467a36d5254> in <module>()
1 ftr_mtrx_custmr, features_defs = ft.dfs(entities=entities,
2 relationships=relationshp,
----> 3 target_entity="transactions")
4 frames
/usr/local/lib/python3.6/dist-packages/featuretools/utils/entry_point.py
in function_wrapper(*args, **kwargs)
38 ep.on_error(error=e,
39 runtime=runtime)
---> 40 raise e
41
42 # send return value
/usr/local/lib/python3.6/dist-packages/featuretools/utils/entry_point.py
in function_wrapper(*args, **kwargs)
30 # call function
31 start = time.time()
---> 32 return_value = func(*args, **kwargs)
33 runtime = time.time() - start
34 except Exception as e:
/usr/local/lib/python3.6/dist-packages/featuretools/synthesis/dfs.py
in dfs(entities, relationships, entityset, target_entity, cutoff_time,
instance_ids, agg_primitives, trans_primitives,
groupby_trans_primitives, allowed_paths, max_depth, ignore_entities,
ignore_variables, primitive_options, seed_features, drop_contains,
drop_exact, where_primitives, max_features, cutoff_time_in_index,
save_progress, features_only, training_window, approximate,
chunk_size, n_jobs, dask_kwargs, verbose, return_variable_types,
progress_callback)
225 '''
226 if not isinstance(entityset, EntitySet):
--> 227 entityset = EntitySet("dfs", entities, relationships)
228
229 dfs_object = DeepFeatureSynthesis(target_entity, entityset,
/usr/local/lib/python3.6/dist-packages/featuretools/entityset/entityset.py
in init(self, id, entities, relationships)
83
84 for relationship in relationships:
---> 85 parent_variable = self[relationship[0]][relationship[1]]
86 child_variable = self[relationship[2]][relationship[3]]
87 self.add_relationship(Relationship(parent_variable,
/usr/local/lib/python3.6/dist-packages/featuretools/entityset/entityset.py
in getitem(self, entity_id)
124 return self.entity_dict[entity_id]
125 name = self.id or "entity set"
--> 126 raise KeyError('Entity %s does not exist in %s' % (entity_id, name))
127
128 #property
however, this returned KeyError : 'Entity c does not exist in dfs'
any idea what's wrong with my code?

interactive slider to turn on LED using pyfirmata

Trying to turn on an LED using an interactive slider using pyfirmata library. I simply want to turn it off or on using the slider. Here is my code (I do not know what I'm doing wrong):
import pyfirmata
import time
from ipywidgets import widgets, interact
import ipywidgets as widgets
from IPython.display import display
pin=9
port='com5'
board = pyfirmata.Arduino(port)
def ledON():
board.digital[pin].write(1)
def ledOFF():
board.digital[pin].write(0)
def runit(LED=ledwidget.result):
if LED == 1:
ledON()
elif LED == 0:
ledOFF()
return(LED)
slider = widgets.IntSlider(value=0,min=0,max=1,step=1)
ledwidget = widgets.interactive (runit,a=slider)
display(ledwidget)
board.exit()
Here is the stack error trace to help in debugging, however I had to cut most of it since I wasn't able to post all of the stack onto here:
SerialException Traceback (most recent call last) <ipython-input-115-6a0aba5925b9> in runit(LED)
19 ledON()
20 elif LED == 0:
---> 21 ledOFF()
22 return(LED)
23
<ipython-input-115-6a0aba5925b9> in ledOFF()
13
14 def ledOFF():
---> 15 board.digital[pin].write(0)
16
17 def runit(LED=ledwidget.result):
C:\Users\trinh\Anaconda3\lib\site-packages\pyfirmata\pyfirmata.py in write(self, value)
531 if self.mode is OUTPUT:
532 if self.port:
--> 533 self.port.write()
534 else:
535 msg = bytearray([DIGITAL_MESSAGE, self.pin_number, value])
C:\Users\trinh\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py in run_code(self, code_obj, result) 2879 self.hooks.pre_run_code_hook() 2880
#rprint('Running code', repr(code_obj)) # dbg
-> 2881 exec(code_obj, self.user_global_ns, self.user_ns) 2882 finally: 2883 # Reset our crash handler in place
C:\Users\trinh\Anaconda3\lib\site-packages\serial\serialwin32.py in write(self, data)
302 """Output the given byte string over the serial port."""
303 if not self.is_open:
--> 304 raise portNotOpenError
305 #~ if not isinstance(data, (bytes, bytearray)):
306 #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
SerialException: Attempting to use a port that is not open

Resources