在 PyTorch 中,我编写了一个非常简单的 CNN 判别器并对其进行了训练。现在我需要部署它来进行预测。但目标机器的 GPU 内存较小,并出现内存不足错误。所以我认为我可以设置requires_grad = False阻止 PyTorch 存储梯度值。但我没有发现它有任何区别。
我的模型中有大约 500 万个参数。但在预测单批输入时,会消耗约 1.2GB 的内存。我想应该不需要这么大的内存。
问题是当我只想使用模型进行预测时如何节省 GPU 内存使用量?
这是一个演示,我用它discriminator.requires_grad_来禁用/启用所有参数的自动分级。但好像并没有什么用。
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as functional
from pynvml.smi import nvidia_smi
nvsmi = nvidia_smi.getInstance()
def getMemoryUsage():
usage = nvsmi.DeviceQuery("memory.used")["gpu"][0]["fb_memory_usage"]
return "%d %s" % (usage["used"], usage["unit"])
print("Before GPU Memory: %s" % getMemoryUsage())
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
# trainable layers
# input: 2x256x256
self.conv1 = nn.Conv2d(2, 8, 5, padding=2) # …Run Code Online (Sandbox Code Playgroud) 我想检查一个对象是否是某个类的实例。在 Python 中,我可以使用isinstance(obj, cls). 在 C/C++ 中,我发现了一个名为PyObject_IsInstance的函数。但它似乎不起作用isinstance。
详细(也称为下面的示例代码):
My。类型定义为MyType,对象定义为MyObject。MyType到名为 name 的导出模块My。my = My(),并isinstance(my, My)返回True。PyObject_IsInstance(my, (PyObject*)&MyType)检查my,并且返回0,这意味着my不是由定义的类的实例MyType。完整的C++代码:
#define PY_SSIZE_T_CLEAN
#include <python3.6/Python.h>
#include <python3.6/structmember.h>
#include <stddef.h>
typedef struct {
PyObject_HEAD
int num;
} MyObject;
static PyTypeObject MyType = []{
PyTypeObject ret = {
PyVarObject_HEAD_INIT(NULL, …Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList自定义类要从一个活动传递到另一个活动。我制作班级工具Parcelable。然而,每次当我使用传递的 ArrayList 时,我都会得到NullPointException. 所以我决定检查 getParcelableArrayList() 返回值的类,并得到 java.lang.Integer。我想知道如何解释这一点。
第一个活动的代码:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, WeiboBrowseActivity.class);
intent.putParcelableArrayListExtra(WeiboBrowseActivity.KEY_WEIBO_INFO_LIST, weiboInfoList);
intent.putExtra(WeiboBrowseActivity.KEY_SELECTED_WEIBO_INDEX, arg2);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
}
Run Code Online (Sandbox Code Playgroud)
第二个活动的代码:
private void initWeiboInfos() {
Log.d("cosmo","the type is: " + getIntent().getExtras().get(KEY_WEIBO_INFO_LIST).getClass().getName());
weiboInfos = getIntent().getExtras().getParcelableArrayList(KEY_WEIBO_INFO_LIST);
if (weiboInfos == null) {
Log.d("cosmo", "weiboInfos null"); //I got this log
} else {
Log.d("cosmo", "weiboInfos not null");
}
}
Run Code Online (Sandbox Code Playgroud)
可打包类的代码: …
我正在按照Python API 的官方教程在 C++ 中为 Python 创建一个简单的扩展类型。但是我无法成功编译我的代码。因为当我T_INT在我的代码中使用时,我收到一个错误说'T_INT' was not declared in this scope。我忘了什么吗?我在教程中找不到答案。
这是我的 C++ 代码:
#define PY_SSIZE_T_CLEAN
#include <python3.6/Python.h>
#include <stddef.h>
typedef struct {
PyObject_HEAD
int num;
} MyObject;
static PyMemberDef MyMembers[] = {
{ "num", T_INT, offsetof(MyObject, num), 0, NULL },
{ NULL }
};
static PyTypeObject MyType = []{
PyTypeObject ret = {
PyVarObject_HEAD_INIT(NULL, 0)
};
ret.tp_name = "cpp.My";
ret.tp_doc = NULL;
ret.tp_basicsize = sizeof(MyObject);
ret.tp_itemsize = 0;
ret.tp_flags = Py_TPFLAGS_DEFAULT; …Run Code Online (Sandbox Code Playgroud) 我正在使用jquery函数attr()来获取输入元素的自定义属性.但是此功能undefined在Chrome中返回.我试图打印这个元素,结果是:
[prevObject: o.fn.init[1], context: document, selector: "input[name="money",uid="1"]", jquery: "2.1.0", constructor: function…]
Run Code Online (Sandbox Code Playgroud)
以下是我的js代码:
$('.panel').each(function(index, element) {
var uid = $(this).attr('uid');
console.log($(this));
var moneyMinusBtn = $(this).find('.btn-money-minus')[0];
var moneyPlusBtn = $(this).find('.btn-money-plus')[0];
var moneyInput = $(this).find('input[name="money"]')[0];
console.log($('input[name="money",uid="'+uid+'"]').attr('uid'));
}
Run Code Online (Sandbox Code Playgroud)
HTML代码:
<input type="number" class="form-control" name="money" value="<?php echo $node['money'] ?>" uid="1" />
Run Code Online (Sandbox Code Playgroud) c++ ×2
python ×2
python-c-api ×2
android ×1
html ×1
java ×1
javascript ×1
jquery ×1
parcelable ×1
python-3.x ×1
pytorch ×1