Comments (7)
Hello @evanlaf, Parsing the first string takes longer as we load the ML model in the backend. This is the expected behaviour of the ML model. The suggested approach is to load the model once and keep the object in RAM to speed up subsequent inferences.
from hawking.
Hi @ajar19 - understood. Where is the model being loaded? I'm wondering if I can write a method to load the model then actually only parse a string when I'm ready, rather than loading the model the first time that the string is being parsed.
from hawking.
@evanlaf For now, we load the model on the first hit. We suggest warming up the model by giving it any basic sentence; that will just do the trick.
private static final HawkingTimeParser PARSER = new HawkingTimeParser(); PARSER.parse("what do you do this month", new Date(), new HawkingConfiguration(),"eng");
from hawking.
@ajar19 Could you clarify where the model is being saved / why this is not working? My steps:
- When application opens, check if config, parser, and parser class is persisted in shared preferences.
- If yes: Pull them and deserialize.
- If no: Initialize objects, parse a test statement, then push objects to shared preferences.
My code:
Step 1: Check if objects are persisted. If yes, restore them (Step 2). If no, call createAndPersistModel()
private fun restoreState() {
// Restore Parser objects
val dateTimeParserJson = application
.getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
.getString("dateTimeParser", null)
val parserConfigJson = application
.getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
.getString("parserConfig", null)
val parserJson = application
.getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
.getString("parser", null)
if (dateTimeParserJson == null || parserConfigJson == null) {
createAndPersistModel()
}
else {
val gs = Gson()
dateTimeParser = gs.fromJson(dateTimeParserJson, HawkingTimeParser::class.java)
parserConfig = gs.fromJson(parserConfigJson, HawkingConfiguration::class.java)
parser = gs.fromJson(parserJson, HawkingParser::class.java)
}
...
}
Step 3: Create and persist the model:
private fun createAndPersistModel() {
dateTimeParser = HawkingTimeParser()
parserConfig = HawkingConfiguration()
parser = HawkingParser() // the class I created to parse strings
parserConfig!!.dateFormat = "mm/dd/yyyy"
val gs = Gson()
GlobalScope.launch {
async(Dispatchers.IO) {
parser?.initParser(dateTimeParser, parserConfig)
application.getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
.edit()
.putString("dateTimeParser", gs.toJson(dateTimeParser))
.putString("parserConfig", gs.toJson(parserConfig))
.putString("parser", gs.toJson(parser))
.apply()
}
}
}
And in HawkingParser.java
, my code:
public void initParser(HawkingTimeParser dateTimeParser, HawkingConfiguration parserConfig) {
Date referenceDate = new Date();
dateTimeParser.parse("Meeting tomorrow at 2pm", referenceDate, parserConfig, "eng");
}
I would have thought that once the model is loaded once, I could save the HawkingTimeParser object and continuously use that each time the app is opened without having to continuously "warm up the model".
I'm looking in HawkingTimeParser.java
and it's also not explicitly clear where the model is being warmed up -- i'm guessing it's in the abstractLanguage.predict(...)
call? What if I were to create an instance of abstractLanguage
in a separate method, pass it into HawkingTimeParser.parse(...)
, and write abstractLanguage
to shared preferences to avoid delays?
public DatesFound parse(String inputSentence, Date referenceDate, HawkingConfiguration config, String lang) {
Configuration configuration = new Configuration(config);
ConfigurationConstants.setConfiguration(configuration);
List<ParserOutput> parserOutputs = new ArrayList<>();
List<DateGroup> dateGroups = new ArrayList<>();
DatesFound datesFound = new DatesFound();
AbstractLanguage abstractLanguage = LanguageFactory.getLanguageImpl(lang);
assert abstractLanguage != null;
List<DateTimeProperties> dateList = abstractLanguage.predict(inputSentence, referenceDate, config);
for (DateTimeProperties date : dateList) {
parserOutputs.add(date.getParserOutput());
dateGroups.add(date.getDateGroup());
}
parserOutputs = DateTimeProperties.addDefaultTime(parserOutputs, config.getDayhourStart(), config.getDayhourEnd());
datesFound.setParserOutputs(parserOutputs);
datesFound.setDateGroups(dateGroups);
LOGGER.info(datesFound.toString());
return DateTimeProperties.emptyDatesRemover(datesFound);
}
from hawking.
@ajar19 Hi, just wanted to bump the question above. Thanks
from hawking.
I'm sorry, @evanlaf, for not getting back to you sooner. What you raised was a valid one. We'll add the option for users to load the model independently. Currently, the model loading process is deeply integrated into our system. We will write a util where users can give the model object and sentence together to make a prediction.
from hawking.
No worries, I ended up going the original workaround you suggested (warming up the model with a test sentence), since I couldn't get the shared preferences to work. I'll keep an eye out for that util when it's created. Thanks!
from hawking.
Related Issues (12)
- A couple of things don't seem quite right HOT 7
- Some observations that failed: HOT 7
- from until parsing HOT 5
- Is there a python wrapper for this ? HOT 1
- A couple of edge cases don't appear to be coming through correctly HOT 4
- Cannot parse: "10/28/2021" or "10-28-2021" HOT 2
- Is there a non-GPL3 option HOT 1
- Relative input truncates the less specific details of start to 0 HOT 1
- Increase In Heap Memory due to "wordToSubstrings" HOT 1
- CVE-2020-15250 vulnerability HOT 1
- One day difference HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hawking.