Chainer: AttributeError: module 'chainer.distributions' has no attribute 'Independent' - chainer

Problem on using API named Independent.
I am using --
Chainer: 5.4.0
NumPy: 1.15.4
CuPy:
CuPy Version : 5.4.0
CUDA Root : /usr/local/cuda-10.0
CUDA Build Version : 10000
CUDA Driver Version : 10010
CUDA Runtime Version : 10000
cuDNN Build Version : 7401
cuDNN Version : 7401
NCCL Build Version : 2307
NCCL Runtime Version : 2307
iDeep: 2.0.0.post3
Does in version 5.4.0 in Chainer.distribution- Class Independent is removed ?
I tried to use the distribution class directly although requirement of Independent class was required.
I am using the below link model.
VAE Network
I am not using Binary dataset and changed according to my own custom dataset.
chainer/network.py in forward(self, x)
22
23 def forward(self, x):
---> 24 q_z = self.encoder(x)
25 z = q_z.sample(self.k)
26 p_x = self.decoder(z)
~/anaconda3/envs/chainer_p36/lib/python3.6/site-packages/chainer/link.py in __call__(self, *args, **kwargs)
240 if forward is None:
241 forward = self.forward
--> 242 out = forward(*args, **kwargs)
243
244 # Call forward_postprocess hook
chainer/network.py in forward(self, x)
50 mu = self.mu(h)
51 ln_sigma = self.ln_sigma(h) # log(sigma)
---> 52 return D.Independent(D.Normal(loc=mu, log_scale=ln_sigma))
53
54
AttributeError: module 'chainer.distributions' has no attribute 'Independent'

D.Independent is a new feature introduced in Chainer v6.0.0. You need to update Chainer.

Related

ERROR: vars() argument must have __dict__ attribute when trying to use trainer.train() on custom HF dataset?

I have the following model that I am trying to fine-tune (CLIP_ViT + classification head). Here’s my model definition:
class CLIPNN(nn.Module):
def __init__(self, num_labels, pretrained_name="openai/clip-vit-base-patch32", dropout=0.1):
super().__init__()
self.num_labels = num_labels
# load pre-trained transformer & processor
self.transformer = CLIPVisionModel.from_pretrained(pretrained_name)
self.processor = CLIPProcessor.from_pretrained(pretrained_name)
# initialize other layers (head after the transformer body)
self.classifier = nn.Sequential(
nn.Linear(512, 128, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(p=dropout, inplace=False),
nn.Linear(128, self.num_labels, bias=True))
def forward(self, inputs, labels=None, **kwargs):
logits = self.classifier(inputs)
loss = None
if labels is not None:
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
return SequenceClassifierOutput(
loss=loss,
logits=logits,
)
I also have the following definition for a dataset:
class CLIPDataset(nn.utils.data.Dataset):
def __init__(self, embeddings, labels):
self.embeddings = embeddings
self.labels = labels
def __getitem__(self, idx):
item = {"embeddings": nn.Tensor(self.embeddings[idx])}
item['labels'] = nn.LongTensor([self.labels[idx]])
return item
def __len__(self):
return len(self.labels)
Note: here I am assuming that the model is fed pre-computed embeddings and does not compute embeddings, I know this is not the right logic if I want to fine-tune the CLIP base model, I am just trying to get my code to work.
Something like this throws an error:
model = CLIPNN(num_labels=2)
train_data = CLIPDataset(train_data, y_train)
test_data = CLIPDataset(test_data, y_test)
trainer = Trainer(
model=model, args=training_args, train_dataset=train_data, eval_dataset=test_data
)
trainer.train()
TypeError Traceback (most recent call last) in
----> 1 trainer.train()
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/transformers/trainer.py
in train(self, resume_from_checkpoint, trial, ignore_keys_for_eval,
**kwargs) 1256 self.control = self.callback_handler.on_epoch_begin(args, self.state, self.control)
1257 → 1258 for step, inputs in enumerate(epoch_iterator): 1259 1260 #
Skip past any already trained steps if resuming training
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/torch/utils/data/dataloader.py in next(self) 515 if self._sampler_iter is None: 516 self._reset() →
517 data = self._next_data() 518 self._num_yielded += 1 519 if
self._dataset_kind == _DatasetKind.Iterable and \
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self) 555 def _next_data(self): 556 index =
self._next_index() # may raise StopIteration → 557 data =
self._dataset_fetcher.fetch(index) # may raise StopIteration 558 if
self._pin_memory: 559 data = _utils.pin_memory.pin_memory(data)
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py
in fetch(self, possibly_batched_index) 45 else: 46 data =
self.dataset[possibly_batched_index] —> 47 return
self.collate_fn(data)
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/transformers/data/data_collator.py
in default_data_collator(features, return_tensors) 64 65 if
return_tensors == “pt”: —> 66 return
torch_default_data_collator(features) 67 elif return_tensors == “tf”:
68 return tf_default_data_collator(features)
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/transformers/data/data_collator.py
in torch_default_data_collator(features) 80 81 if not
isinstance(features[0], (dict, BatchEncoding)): —> 82 features =
[vars(f) for f in features] 83 first = features[0] 84 batch = {}
~/anaconda3/envs/pytorch_latest_p37/lib/python3.7/site-packages/transformers/data/data_collator.py
in (.0) 80 81 if not isinstance(features[0], (dict, BatchEncoding)):
—> 82 features = [vars(f) for f in features] 83 first = features[0] 84
batch = {}
TypeError: vars() argument must have dict attribute
any idea what I'm doing wrong?

Why this parametric constructor only works with multiline definition?

I have a Device constructor that only works if I define it using the multiline function syntax, but it fails if I define it with the one line function syntax, with the following error:
ERROR: LoadError: UndefVarError: F not defined (if I remove F) or
ERROR: LoadError: UndefVarError: C not defined
However it also works if I only remove the F and C type parameters from the method signature (but not the where {C<:UxnCpu, F<:Function} part).
69 abstract type AbstractCPU end
70 mutable struct Device{C<:AbstractCPU, F<:Function}
71 cpu::C
72 id_addr::Int
73 vector::UInt16
74 talk::F
75 mem::OVector{UInt8}
76 dat::OVector{UInt8}
77 end
78
79 function Device(cpu::C, id_addr::Int, talkfn::F)::Device where {C<:AbstractCPU, F<:Function}
80 d = Device(cpu, id_addr, 0x000, talkfn, cpu.ram.dat, OVector{UInt8}(0x10))
81 cpu.dev[id_addr] = d
82
83 return d
84 end
85
---> 86 Device(talkfn::F, cpu::C, id_addr::Int)::Device where {C<:AbstractCPU, F<:Function} = Device(cpu, id_addr, talkfn)
87
88 Device(cpu, id_addr::Int) where {C<:AbstractCPU} = begin
89 Device(cpu, id_addr) do d::Device, b0::UInt8, w::UInt8
90 return true
91 end
92 end
The full stack trace is:
ERROR: LoadError: UndefVarError: F not defined
Stacktrace:
[1] top-level scope
# C:\Users\ismae\Bin\uxn-dev\src\VarvaraEmulator.jl:86 <---
[2] include(fname::String)
# Base.MainInclude .\client.jl:444
[3] top-level scope
# REPL[6]:1
in expression starting at C:\Users\ismae\Bin\uxn-dev\src\VarvaraEmulator.jl:25
However this workarounds do work:
Device(talkfn, cpu, id_addr::Int)::Device where {C<:AbstractCPU, F<:Function} = Device(cpu, id_addr, talkfn)
I would have thought it would complain with something about to few type parameters in signature.
The following syntax doesn't work either:
Device(talkfn::F, cpu::C, id_addr::Int)::Device where {C<:AbstractCPU, F<:Function} = begin
Device(cpu, id_addr, talkfn)
end
However this does, which is what I left in the end:
function Device(talkfn::F, cpu::C, id_addr::Int)::Device where {C<:AbstractCPU, F<:Function}
Device(cpu, id_addr, talkfn)
end
I would like to understand where that error is coming from, what am I doing wrong?
I tried to replicate it in a minimal example, but I was not able to, here is my the complete code for reference:
https://github.com/Ismael-VC/VarvaraEmulator.jl/blob/VarvaraEmulator.jl/src/VarvaraEmulator.jl#L69-L99
This is my current setup:
julia> versioninfo()
Julia Version 1.6.3
Commit ae8452a9e0 (2021-09-23 17:34 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-4710HQ CPU # 2.50GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, haswell)
Environment:
JULIA_LOAD_PATH = C:\Users\\smae\Bin\uxn-dev\src;
(#v1.6) pkg> status
Status `C:\Users\ismae\.julia\environments\v1.6\Project.toml`
[6e4b80f9] BenchmarkTools v1.2.0
[c43c736e] Genie v3.2.0
[7eb4fadd] Match v1.1.0
[6fe1bfb0] OffsetArrays v1.10.7
[c3e4b0f8] Pluto v0.16.1
[b873ce64] ReplMaker v0.2.5
[98e33af6] SimpleDirectMediaLayer v0.3.0
[90137ffa] StaticArrays v1.2.13
Best regards.
I think it's just an issue parsing the parameters when there is a return type annotation, not your fault. I can replicate the error with a much more minimal example.
These work:
function f1(x::F, y::C) where {F<:Integer, C<:Complex}
1
end
function f2(x::F, y::C)::Int where {F<:Integer, C<:Complex}
2
end
g1(x::F, y::C) where {F<:Integer, C<:Complex} = 3
But this doesn't:
g2(x::F, y::C)::Int where {F<:Integer, C<:Complex} = 4
Edit: From the comments, the last case can be fixed by adding parentheses:
(g2(x::F, y::C)::Int) where {F<:Integer, C<:Complex} = 4

IndexError: index out of range in self while try to fine tune Roberta model after adding special tokens

I am trying to fine tune a Roberta model after adding some special tokens to its tokenizer:
special_tokens_dict = {'additional_special_tokens': ['[Tok1]','[Tok2]']}
tokenizer.add_special_tokens(special_tokens_dict)
I get this error when i try to train the model (on cpu):
IndexError Traceback (most recent call last)
<ipython-input-75-d63f8d3c6c67> in <module>()
50 l = model(b_input_ids,
51 attention_mask=b_input_mask,
---> 52 labels=b_labels)
53 loss,logits = l
54 total_train_loss += l[0].item()
8 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
1850 # remove once script supports set_grad_enabled
1851 _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1852 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
1853
1854
IndexError: index out of range in self
p.s. If I comment add_special_tokens the code works.
You also need to tell your model that it needs to learn the vector representations of two new tokens:
from transformers import RobertaTokenizer, RobertaForQuestionAnswering
t = RobertaTokenizer.from_pretrained('roberta-base')
m = RobertaForQuestionAnswering.from_pretrained('roberta-base')
#roberta-base 'knows' 50265 tokens
print(m.roberta.embeddings.word_embeddings)
special_tokens_dict = {'additional_special_tokens': ['[Tok1]','[Tok2]']}
t.add_special_tokens(special_tokens_dict)
#we now tell the model that it needs to learn new tokens:
m.resize_token_embeddings(len(t))
m.roberta.embeddings.word_embeddings.padding_idx=1
print(m.roberta.embeddings.word_embeddings)
Output:
Embedding(50265, 768, padding_idx=1)
Embedding(50267, 768, padding_idx=1)

ECEF frames in astropy/healpix?

I went through the astropy documentation and concluded that
there are no native ECEF (earth-centered, earth-fixed) frames with lon, lat coordinates that can be converted into equatorial coordinates RA, dec if the time is given. Is this true?
Eventually, I'd like to create a map using:
map = HEALPix(nside=NSIDE, order='nested', frame=MY_REF_FRAME())
MY_REF_FRAME = ITRS is apparently not an option.
I'd be grateful if someone can help me to find if there is a way this can be defined with a recent version of astropy.
Thanks!
Eric
HEALPix itself and https://astropy-healpix.readthedocs.io is mostly about the HEALPix pixels in one given sky frame.
For transformations between different sky coordinate systems you should look to astropy.coordinates.
I'm not familiar with ECEF, but you might be able to do the computation you want using the existing ICRS and ITRF frames (see https://stackoverflow.com/a/49325584/498873).
Let me post an answer to last #Christoph comment here so that I can paste the full code. This is the code and error I get:
In [1]: import astropy.coordinates as coord
In [2]: import astropy.units as u
In [3]: from astropy.time import Time
In [4]: coord.ITRS( coord.SphericalRepresentation(lon= 0.0 *u.deg ,lat = 0.0 * u.deg, distance = 1 * u.m), obstime=Time('2018-12-03 14:00:00')).transform_to(coord.ICRS)
ValueError Traceback (most recent call last)
<ipython-input-4-1d50da4d3855> in <module>
----> 1 coord.ITRS( coord.SphericalRepresentation(lon= 0.0 *u.deg ,lat = 0.0 * u.deg, distance = 1 * u.m), obstime=Time('2018-12-03 14:00:00')).transform_to(coord.ICRS)
~/lib/python3.5/site-packages/astropy/coordinates/baseframe.py in transform_to(self, new_frame)
1165 msg = 'Cannot transform from {0} to {1}'
1166 raise ConvertError(msg.format(self.__class__, new_frame.__class__))
-> 1167 return trans(self, new_frame)
1168
1169 def is_transformable_to(self, new_frame):
...
474 # In case we want to convert 1e20 to int.
475 try:
--> 476 fill_value = np.array(fill_value, copy=False, dtype=ndtype)
477 except OverflowError:
478 # Raise TypeError instead of OverflowError.
ValueError: invalid literal for int() with base 10: 'N'
In [6]: astropy.__version__
Out[6]: '3.0.5'
In [8]: numpy.__version__
Out[8]: '1.15.3'

1D convolution sequence in keras

I am a noob trying to build a network to classify 2 sequences of floats to one of 16450 different integers. I have 70408 samples and I have padded each sample to have 1400 values. So 1 sample has 2 column vectors eg. [104.243,120.12...], [125.25,14.556...]. Both my x_train is size (70408,1400). I am trying to use keras' functional API but can't seem to figure out the right input shape. Any help would be appreciated.
samples = 70408
mass_size = 1400
intensity_size = 1400
output_size = 16450
mass_input = Input(shape=(samples,mass_size), dtype='float32')
mass_net = layers.Conv1D(32,5,activation='relu')(mass_input)
mass_net = layers.AveragePooling1D(3)(mass_net)
mass_net = layers.Conv1D(16,5,activation='relu')(mass_net)
mass_net = layers.GlobalAveragePooling1D()(mass_net)
intensity_input = Input(shape=(samples,intensity_size), dtype='float32')
intensity_net = layers.Conv1D(32,5,activation='relu')(intensity_input)
intensity_net = layers.AveragePooling1D(3)(intensity_net)
intensity_net = layers.Conv1D(16,5,activation='relu')(intensity_net)
intensity_net = layers.GlobalAveragePooling1D()(intensity_net)
concatenated = layers.concatenate([mass_net,intensity_net],axis=-1)
output = layers.Dense(output_size,activation='softmax')(concatenated)
print(mass_data.shape, intensity_data.shape)
model = Model([mass_data,intensity_data],output)
model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc'])
model.fit([mass_data,intensity_data],y_train,epochs=10,batch_size=128)
The error I keep getting is:
TypeError Traceback (most recent call last)
<ipython-input-18-aab93c439dd0> in <module>()
28
29 print(mass_data.shape, intensity_data.shape)
---> 30 model = Model([mass_data,intensity_data],output)
31 model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['acc'])
32
~\Anaconda3\envs\deeplearning\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~\Anaconda3\envs\deeplearning\lib\site-packages\keras\engine\topology.py in __init__(self, inputs, outputs, name)
1528
1529 # Check for redundancy in inputs.
-> 1530 if len(set(self.inputs)) != len(self.inputs):
1531 raise ValueError('The list of inputs passed to the model '
1532 'is redundant. '
TypeError: unhashable type: 'numpy.ndarray'
The problem seems to be here:
model = Model([mass_data,intensity_data],output)
You should use the input tensors you created, not numpy data:
model = Model([mass_input, intensity_input],output)
Another problem, related to my old comment is the input_shape.
Since you now have your data as (samples, length, features), you need input_shape=(length,features)

Resources