You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
so future. we could. do Graph Neural Networks build by javacpp-pytorch , it is very cool . these days. I. just clean code and want to release with javacpp-pytorch 2.9.1-1.5.13 . after I will git push the code
and. when I. write the Graph Neural Networks ,them may need Parameter object , I know in libtorch not implement Parameter , but Parameter is often use in python . https://github.com/pytorch/pytorch/blob/main/torch/nn/parameter.py.
so could. we. implement nn.parameter in javacpp-pytorch. we also have ParameterListImpl and ParameterDictImpl ,
maybe we could write code like that
package org.example.torch.nn;
import org.bytedeco.pytorch.Tensor;
import org.bytedeco.pytorch.global.torch;
/**
* 模仿 Python PyTorch 的 torch.nn.Parameter 实现。
* <p>
* Parameter 是 Tensor 的子类,当它们作为属性分配给 Module 时,
* 它们会被自动添加到参数列表中。
*/
public class Parameter extends Tensor {
public Parameter(Tensor data) {
this(data, true);
}
public Parameter(Tensor data, boolean requiresGrad) {
super(data != null ? data.detach() : new Tensor());
if (requiresGrad) {
this.set_requires_grad(true);
}
}
public Tensor data() {
return this;
}
@Override
public String toString() {
return "Parameter containing:\n" + super.toString();
}
}
import org.bytedeco.pytorch.Tensor;
import org.bytedeco.pytorch.global.torch;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class MyLinearLayer {
// 使用我们需要转译的 Parameter 类型
public Parameter weight;
public Parameter bias;
public MyLinearLayer(int inFeatures, int outFeatures) {
// 初始化权重 (Xavier/Kaiming init 均可,这里用 randn 演示)
Tensor w = torch.randn(outFeatures, inFeatures);
Tensor b = torch.zeros(outFeatures);
// 包装成 Parameter
this.weight = new Parameter(w, true);
this.bias = new Parameter(b, true);
}
// 模拟 PyTorch 的 parameters() 方法
public List<Parameter> parameters() {
List<Parameter> params = new ArrayList<>();
// 使用反射扫描当前类中所有的 Parameter 字段
for (Field field : this.getClass().getDeclaredFields()) {
if (Parameter.class.isAssignableFrom(field.getType())) {
try {
params.add((Parameter) field.get(this));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return params;
}
public Tensor forward(Tensor input) {
// Parameter 可以直接当作 Tensor 使用
return torch.addmm(this.bias, input, this.weight.t());
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
HI @saudet
I have one big news want to share with you , now I have implemnet gym framework with javacpp-pytorch by pure java code, it really could work , https://pyg.org/ Graph Neural Networks https://github.com/pyg-team/pytorch_geometric
so future. we could. do Graph Neural Networks build by javacpp-pytorch , it is very cool . these days. I. just clean code and want to release with javacpp-pytorch 2.9.1-1.5.13 . after I will git push the code
and. when I. write the Graph Neural Networks ,them may need Parameter object , I know in libtorch not implement Parameter , but Parameter is often use in python .
https://github.com/pytorch/pytorch/blob/main/torch/nn/parameter.py.
so could. we. implement nn.parameter in javacpp-pytorch. we also have ParameterListImpl and ParameterDictImpl ,
maybe we could write code like that
Beta Was this translation helpful? Give feedback.
All reactions