fromrayimporttunedefobjective(step, alpha, beta):
return (0.1+alpha*step/100)**(-1) +beta*0.1deftraining_function(config):
# Hyperparametersalpha, beta=config["alpha"], config["beta"]
forstepinrange(10):
# Iterative training function - can be any arbitrary training procedure.intermediate_score=objective(step, alpha, beta)
# Feed the score back back to Tune.tune.report(mean_loss=intermediate_score)
analysis=tune.run(
training_function,
config={
"alpha": tune.grid_search([0.001, 0.01, 0.1]),
"beta": tune.choice([1, 2, 3])
})
print("Best config: ", analysis.get_best_config(metric="mean_loss", mode="min"))
# Get a dataframe for analyzing trial results.df=analysis.results_df
>>> from jnius import autoclass
>>> autoclass('java.lang.System').out.println('Hello world')
Hello world
>>> Stack = autoclass('java.util.Stack')
>>> stack = Stack()
>>> stack.push('hello')
>>> stack.push('world')
>>> print(stack.pop())
world
>>> print(stack.pop())
hello
当你引入类后,你只需要按 Java 的函数操作即可,如上述代码中的 push 和 pop 函数。
最令人惊喜的是,你还能在安卓系统中利用这个模块使用Python调用Java类:
from time import sleep
from jnius import autoclass
Hardware = autoclass('org.renpy.android.Hardware')
print('DPI is', Hardware.getDPI())
Hardware.accelerometerEnable(True)
for x in xrange(20):
print(Hardware.accelerometerReading())
sleep(.1)