我使用 prediction.trainedmodels.insert 在谷歌预测 api 中创建了一个空模型。我提供了空的存储位置和训练实例。它已成功创建。
现在我正在尝试使用 Google Prediction Java API 1.6 使用新的训练实例来更新模型。
这是代码
public class PredictionSample {
private static final String APPLICATION_NAME = "MyApplication";
static final String MODEL_ID = "3";
static final String STORAGE_DATA_LOCATION = "";
private static final java.io.File DATA_STORE_DIR =
new java.io.File(System.getProperty("user.home"), ".store/prediction_sample");
private static FileDataStoreFactory dataStoreFactory;
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static Credential authorize() throws Exception {
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(new FileInputStream(new File("client_secrets.json"))));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(PredictionScopes.PREDICTION)).setDataStoreFactory(
dataStoreFactory).build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
private static void run() throws Exception {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// authorization
Credential credential = authorize();
Prediction prediction = new Prediction.Builder(
httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
update(prediction);
predict(prediction, "Camera is bad");
predict(prediction, "Camera is good");
predict(prediction, "Camera is awesome");
}
private static void update(Prediction prediction) throws IOException {
Update update = new Update();
List<Object> csvInstance = new ArrayList<Object>();
csvInstance.add("camera is awesone");
update.setCsvInstance(csvInstance);
update.setOutput("postive");
prediction.trainedmodels().update("<project-no>" , MODEL_ID, update).execute();
System.out.println("Update started.");
System.out.print("Waiting for update to complete");
System.out.flush();
int triesCounter = 0;
while (triesCounter < 100) {
try {
HttpResponse response = prediction.trainedmodels().get("<project-no>",
MODEL_ID).executeUnparsed();
if (response.getStatusCode() == 200) {
System.out.println(response.getStatusMessage());
return;
}
response.ignore();
} catch (HttpResponseException e) {
}
try {
Thread.sleep(1000 * (triesCounter + 1));
} catch (InterruptedException e) {
break;
}
System.out.print(".");
System.out.flush();
triesCounter++;
}
}
private static void predict(Prediction prediction, String text) throws IOException {
Input input = new Input();
InputInput inputInput = new InputInput();
inputInput.setCsvInstance(Collections.<Object>singletonList(text));
input.setInput(inputInput);
Output output =
prediction.trainedmodels().predict("<project-no>", MODEL_ID, input).execute();
System.out.println("Text: " + text);
System.out.println("Predicted language: " + output.getOutputLabel());
}
public static void main(String[] args) {
try {
run();
// success!
return;
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
}
此代码给出以下输出。
Update started.
Waiting for Update to complete
OK
400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"location" : "q",
"locationType" : "parameter",
"message" : "Model is empty. Please update it first.",
"reason" : "invalidQuery"
} ],
"message" : "Model is empty. Please update it first."
}
当我在更新模型时得到 OK 和状态码 200 时,为什么它说模型是空的。请帮助我。