How to use Bart with PretrainedTransformerEmbedder? - allennlp

if i use encoder = PretrainedTransformerEmbedder(model_name, sub_module="encoder") as the encoder to pass to Bart(encoder=encoder), it reports error because it doesn't implement get_input_dim(), if i pass encoder = PretrainedTransformerEmbedder(model_name, sub_module="encoder"), encoder = encoder.encoder as the input encoder like metioned here, it reports error because the PretrainedTransformerEmbedder(model_name, sub_module="encoder") doesn't has an attribute encoder.
So how can i use the full bart model(including token_embed, position_embed) for seq2seq task in allennlp?

If you just pass encoder=None (which is the default), the Bart model will use the native BART encoder. It sounds like that's what you want?

Related

AttributeError: 'collections.OrderedDict' object has no attribute 'predict'

Being a new guy and a beginner to deep learning and pytorch I am not sure what all inputs should I give you guys to answer my question. But I will try my best to make you guys understand my problem. I have loaded a model in pytorch using 'model= torch.load('model/resnet18-5c106cde.pth')'. But it is showing an AttributeError: 'collections.OrderedDict' object has no attribute 'predict', when I used the command 'prediction = model.predict(test_image)'. Hope you guys understood my problem and Thanks in advance...
I'd guess that the checkpoint you are loading stores a model state dict (the model's parameters) rather than a model (the structure of the model plus its parameters). Try:
model = resnet18(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
model.eval()
where PATH is the path to the model checkpoint. You need to declare model as an instance of the object class (declare the model structure) so that you can load the checkpoint (parameters only, no structure). So you'll need to find the appropriate class to import for the resnet18, probably something along the lines of:
from torchvision.models import resnet18

Serialize a polymorphic F case class using Play Json

I have a situation where I have to have this case class Config[F[_]](pattern: String, format:F[String]), because sometimes the format should be present, and use it like Config[Id] and sometimes not and make it with Config[Option].
The question is, how is this coping with Play or Spray Json and what are some best practices to serialize / deseralize such a structure.
I use to use this trick few times before, but never forced to serialized until and I wonder how read and write methods should look like. Also, if there are any drawback or penalties, performance wise as well.
Any thoughts? Thanks, folks!
First of all, if derivation is not made bad, you would be able to make codecs for Config[List] and Config[Option].
In circe it should be like this:
implicit val configOptionCodec: Codec[Config[Option]] = deriveCodec
implicit val configListCodec: Codec[Config[List]] = deriveCodec
This won't give you much performance penalty, just boilerplate penalty. However you can write macro like #JsonCodecsFor(List, Option, Chain).

Json Argonaut Too Big For Case Class

There's an API I have no influence on that has a JSON result object with a member that has 23 fields. The paradigm of case classes doesn't work, because there's a limit of 22. I've seen Slick and other libraries use HLists to resolve this. Is there a way to do this in Argonaut? If so, please give me sample piece of code to leverage. Thank you!
object BusinessResults{
implicit def BusinessResultsCodecJson: CodecJson[BusinessResults] =
casecodec23(BusinessResults.apply, BusinessResults.unapply)( /**... 23 fields ...**/)
}
I did not create an elegant solution for this. I just hand jammed a 23 piece for-comprehension to create the decoder.

f# class constructor formatting

I am creating a program that takes an email and converts it into json. In the interest of learning functional languages I thought it would be a good opportunity to learn F#. So first off I want a message class that can then easily be serialized into json. This class will have some members that can be null. Because of these optional parameters I am using the following format
type Email(from:string, recipient:string, ?cc:string .........) =
member x.From = from
member x.Recipient = recipient.....
This class actually has a lot of members as I want to store every piece of information that an email could have. What happens is if I try to format the constructor myself by splitting it onto multiple lines I get warnings. How do people make such a gigantic constructor look good in f#? Is there a better way to do this?
On a similar note, I'm not finding visual studio very helpful for f#, what are other people using to code? I think there is a mode for emacs for example...
There should not be any warnings if all "newlined" parameters start at the same column. For example:
type Email(from:string, recipient:string,
?cc:string) =
member x.From = from
member x.Recipient = recipient
(I personally enjoy using Visual Studio for F#. I doubt there is an IDE with more complete support.)
As an alternative to creating constructor that takes all properties as parameters, you can also create an object with mutable properties and then specify only those that you want to set in the construction:
type Email(from:string, recipient:string) =
member val From = from with get, set
member val Recipient = recipient with get, set
member val Cc = "" with get, set
member val Bcc = "" with get, set
let eml = Email("me#me.com", "you#you.com", Cc="she#she.net")
This is using a new member val feature of F# 3.0. Writing the same in F# 2.0 would be pretty annoying, because you'd have to define getter and setter by hand.

How to use mochijson to encode data structure in erlang?

I am using mochiweb and I don't know how to use its json encoder to deal with complex data structure. What's the differences between mochijson and mochijson2? Is there any good example? I always get the following errors:
46> T6={struct,[{hello,"asdf"},{from,"1"},{to,{a,"aa"}}]}.
{struct,[{hello,"asdf"},{from,"1"},{to,{a,"aa"}}]}
47> mochijson2:encode(T6).
** exception exit: {json_encode,{bad_term,{a,"aa"}}}
in function mochijson2:json_encode/2
in call from mochijson2:'-json_encode_proplist/2-fun-0-'/3
in call from lists:foldl/3
in call from mochijson2:json_encode_proplist/2
39> T3={struct,[{hello,"asdf"},{[{from,"1"},{to,"2"}]}]}.
{struct,[{hello,"asdf"},{[{from,"1"},{to,"2"}]}]}
40> mochijson:encode(T3).
** exception error: no function clause matching mochijson:'-json_encode_proplist/2-fun-0-'({[{from,"1"},{to,"2"}]},
[44,"\"asdf\"",58,"\"hello\"",123],
{encoder,unicode,null})
in function lists:foldl/3
in call from mochijson:json_encode_proplist/2
mochijson2 works with strings as binaries, lists as arrays, and only decodes UTF-8. mochijson decodes and encodes unicode code points.
To create a deep struct I did the following:
2> L = {struct, [{key2, [192]}]}.
{struct,[{key2,"?"}]}
3>
3> L2 = {struct, [{key, L}]}.
{struct,[{key,{struct,[{key2,"?"}]}}]}
4>
4> mochijson:encode(L2).
[123,"\"key\"",58,
[123,"\"key2\"",58,[34,"\\u00c0",34],125],
125]
So if you recursively create your data structure using lists then you'll be fine. I'm not sure why deep structs aren't supported, but they don't seem to be, at least not the way you're trying to create them. Maybe someone else knows a more clever way to do this.
You can also check out this thread: mochijson2 examples!
or
Video Tutorial To Start Developing Web Applications on Erlang
T6={struct, [{hello,"asdf"},{from,"1"},{to, {a,"aa"}} ]}.
should instead be:
T6={struct, [{hello,"asdf"},{from,"1"},{to, {struct, [{a,"aa"}]}} ]}.
The nested structures need to have the same "struct" style as the top-level object.