我有一个多次使用np.random的程序。现在,我不想让用户传递参数gpu=True/False
。如何覆盖np.random返回cm.CUDAMatrix(np.random.uniform(low=low, high=high, size=size))
而不以递归结束?
或者有更好的方法来使用cudamat进行小代码更改吗?
感谢您的帮助。
如果您需要更多代码,请发表评论。
class FeedForwardNetwork():
def __init__(self, input_dim, hidden_dim, output_dim, dropout=False, dropout_prop=0.5, gpu=True):
np.random.seed(1)
self.input_layer = np.array([])
self.hidden_layer = np.array([])
self.output_layer = np.array([])
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.dropout = dropout
self.dropout_prop = dropout_prop
r_input_hidden = math.sqrt(6 / (input_dim + hidden_dim))
r_hidden_output = math.sqrt(6 / (hidden_dim + output_dim))
self.weights_input_hidden = np.random.uniform(low=-0.01, high=0.01, size=(input_dim, hidden_dim))
self.weights_hidden_output = np.random.uniform(low=-0.01, high=0.01, size=(hidden_dim, output_dim))
答案 0 :(得分:1)
class FeedForwardNetwork():
def __init__(self, input_dim, hidden_dim, output_dim, dropout=False, dropout_prop=0.5, gpu=True):
np.random.seed(1)
self.input_layer = np.array([])
self.hidden_layer = np.array([])
self.output_layer = np.array([])
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.dropout = dropout
self.dropout_prop = dropout_prop
r_input_hidden = math.sqrt(6 / (input_dim + hidden_dim))
r_hidden_output = math.sqrt(6 / (hidden_dim + output_dim))
self.weights_input_hidden = np.random.uniform(low=-0.01, high=0.01, size=(input_dim, hidden_dim))
self.weights_hidden_output = np.random.uniform(low=-0.01, high=0.01, size=(hidden_dim, output_dim))
def np_random(self, gpu):
'''gpu:bool'''
if gpu:
return np.random.uniform(low=-0.01, high=0.01, size=(self.input_dim, self.hidden_dim))
else:
return np.random.uniform(low=-0.01, high=0.01, size=(self.hidden_dim, self.output_dim))
然后你可以从你的实例中调用它:
instance = FeedForwardNetwork(**kwargs)
instance.np_random(True/False)