<commit_before>require 'nn'
require 'image'
require 'torch'
require 'optim'
require 'sys'
torch.setdefaulttensortype('torch.FloatTensor')

dofile("save_array.lua")

num_feats_in = 5
num_feats_out = 10
width = 10
height = 10
math.mod = math.fmod

data_in = torch.FloatTensor(num_feats_in, height, width)

for f=1,num_feats_in do
  val = f - (width * height) / 16
  for v=1,height do
    for u=1,width do
      data_in[{f,v,u}] = val
      val = val + 1/8
    end
  end
end

saveArray(data_in, "test_data/data_in.bin")
print('Data In: saved to test_data/data_in.bin')

model = nn.Sequential()

-- Test tanh
model:add(nn.Tanh())
res = model:forward(data_in)
saveArray(res, "test_data/tanh_result.bin")
print('Tanh result saved to test_data/tanh_result.bin')

-- Test Threshold
threshold = 0.5
val = 0.1
model:add(nn.Threshold(threshold, val))
res = model:forward(data_in)
saveArray(res, "test_data/threshold.bin")
print('Threshold result saved to test_data/threshold.bin')

-- Test SpatialConvolutionMap
n_states_in = num_feats_in
n_states_out = num_feats_out
fan_in = 3
filt_width = 5
filt_height = 5
conn_table = nn.tables.random(n_states_in, n_states_out, fan_in)
for f_out=1,num_feats_out do
  for f_in=1,fan_in do
    conn_table[{(f_out-1) * fan_in + f_in, 2}] = f_out
    cur_f_in = math.mod((f_out-1) + (f_in-1), n_states_in) + 1
    conn_table[{(f_out-1) * fan_in + f_in, 1}] = cur_f_in
  end
end
-- print('Spatial Convolution Map Connection Table')
-- print(conn_table)
spat_conv_map = nn.SpatialConvolutionMap(conn_table, filt_width, filt_height)
-- print('Spatial Convolution Map Connection Table Rev')
-- print(spat_conv_map.connTableRev)
for f_out=1,num_feats_out do
  spat_conv_map.bias[{f_out}] = f_out / num_feats_out - 0.5
end
-- print('Spatial Convolution Map Biases')
-- print(spat_conv_map.bias)
num_filt = fan_in * num_feats_out
sigma_x_sq = 1
sigma_y_sq = 1
for filt=1,num_filt do
  for v=1,filt_height do
    for u=1,filt_width do
      x = u - 1 - (filt_width-1) / 2
      y = v - 1 - (filt_height-1) / 2
      spat_conv_map.weight[{filt, v, u}] = (filt / num_filt) * math.exp(-((x*x) / (2*sigma_x_sq) + 
        (y*y) / (2*sigma_y_sq)))
    end
  end
end
-- print('Spatial Convolution Map Weights')
-- print(spat_conv_map.weight)
model:add(spat_conv_map)
res = model:forward(data_in)
saveArray(res, "test_data/spatial_convolution_map.bin")
print('Spatial Convolution Map result result saved to test_data/spatial_convolution_map.bin')

-- Test SpatialConvolution
n_states_in = num_feats_in
n_states_out = num_feats_out
fan_in = n_states_in
filt_width = 5
filt_height = 5
spat_conv = nn.SpatialConvolution(n_states_in, n_states_out, filt_width, filt_height)
for f_out=1,num_feats_out do
  spat_conv.bias[{f_out}] = f_out / num_feats_out - 0.5
end
-- print('Spatial Convolution Biases')
-- print(spat_conv.bias)
num_filt = num_feats_out * num_feats_in
sigma_x_sq = 1
sigma_y_sq = 1
for fout=1,num_feats_out do
  for fin=1,num_feats_in do
    for v=1,filt_height do
      for u=1,filt_width do
        filt = (fout-1) * num_feats_out + (fin - 1) + 1
        x = u - 1 - (filt_width-1) / 2
        y = v - 1 - (filt_height-1) / 2
        spat_conv.weight[{fout, fin, v, u}] = (filt / num_filt) * math.exp(-((x*x) / (2*sigma_x_sq) + 
          (y*y) / (2*sigma_y_sq)))
      end
    end
  end
end
-- print('Spatial Convolution Weights')
-- print(spat_conv.weight)
res = spat_conv:forward(model:get(2).output)
saveArray(res, "test_data/spatial_convolution.bin")
print('Spatial Convolution result result saved to test_data/spatial_convolution.bin')

-- Test SpatialLPPooling
pnorm = 2.0
nstates = num_feats_out
poolsize_u = 2
poolsize_v = 2
pool_stage = nn.SpatialLPPooling(nstates, poolsize_u, poolsize_v, poolsize_u, poolsize_v)
model:add(pool_stage)
res = model:forward(data_in)
saveArray(res, "test_data/spatial_lp_pooling.bin")
print('Spatial LP Pooling result result saved to test_data/spatial_lp_pooling.bin')

-- Test SpatialMaxPooling
model3 = nn.Sequential()
max_pool_stage = nn.SpatialMaxPooling(poolsize_u, poolsize_v, poolsize_u, poolsize_v)
model3:add(max_pool_stage)
res = model3:forward(data_in)
saveArray(res, "test_data/spatial_max_pooling.bin")
print('Spatial Max Pooling result result saved to test_data/spatial_max_pooling.bin')

-- Test SpatialSubtractiveNormalization
model4 = nn.Sequential()
normkernel = image.gaussian1D(7)
print('Normalization Kernel1D')
print(normkernel)
norm = nn.SpatialSubtractiveNormalization(num_feats_in, normkernel)
model4:add(norm)
res = model4:forward(data_in)
saveArray(res, "test_data/spatial_subtractive_normalization.bin")
print('SpatialSubtractiveNormalization result result saved to test_data/spatial_subtractive_normalization.bin')

model4 = nn.Sequential()
normkernel = image.gaussian(7)
print('Normalization Kernel2D')
print(normkernel)
norm = nn.SpatialSubtractiveNormalization(num_feats_in, normkernel)
model4:add(norm)
res = model4:forward(data_in)
saveArray(res, "test_data/spatial_subtractive_normalization_2d.bin")
print('SpatialSubtractiveNormalization result result saved to test_data/spatial_subtractive_normalization_2d.bin')

error('early out')

-- Test SpatialDivisiveNormalization
model5 = nn.Sequential()
normkernel = image.gaussian1D(7)
print('Normalization Kernel1D')
print(normkernel)
spatial_div_norm = nn.SpatialDivisiveNormalization(num_feats_in, normkernel)
model5:add(spatial_div_norm)
res = model5:forward(data_in)
saveArray(res, "test_data/spatial_divisive_normalization.bin")
print('SpatialDivisiveNormalization result result saved to test_data/spatial_divisive_normalization.bin')

model5 = nn.Sequential()
normkernel = image.gaussian(7)<commit_msg>remove early out statement.<commit_after>require 'nn'
require 'image'
require 'torch'
require 'optim'
require 'sys'
torch.setdefaulttensortype('torch.FloatTensor')

dofile("save_array.lua")

num_feats_in = 5
num_feats_out = 10
width = 10
height = 10
math.mod = math.fmod

data_in = torch.FloatTensor(num_feats_in, height, width)

for f=1,num_feats_in do
  val = f - (width * height) / 16
  for v=1,height do
    for u=1,width do
      data_in[{f,v,u}] = val
      val = val + 1/8
    end
  end
end

saveArray(data_in, "test_data/data_in.bin")
print('Data In: saved to test_data/data_in.bin')

model = nn.Sequential()

-- Test tanh
model:add(nn.Tanh())
res = model:forward(data_in)
saveArray(res, "test_data/tanh_result.bin")
print('Tanh result saved to test_data/tanh_result.bin')

-- Test Threshold
threshold = 0.5
val = 0.1
model:add(nn.Threshold(threshold, val))
res = model:forward(data_in)
saveArray(res, "test_data/threshold.bin")
print('Threshold result saved to test_data/threshold.bin')

-- Test SpatialConvolutionMap
n_states_in = num_feats_in
n_states_out = num_feats_out
fan_in = 3
filt_width = 5
filt_height = 5
conn_table = nn.tables.random(n_states_in, n_states_out, fan_in)
for f_out=1,num_feats_out do
  for f_in=1,fan_in do
    conn_table[{(f_out-1) * fan_in + f_in, 2}] = f_out
    cur_f_in = math.mod((f_out-1) + (f_in-1), n_states_in) + 1
    conn_table[{(f_out-1) * fan_in + f_in, 1}] = cur_f_in
  end
end
-- print('Spatial Convolution Map Connection Table')
-- print(conn_table)
spat_conv_map = nn.SpatialConvolutionMap(conn_table, filt_width, filt_height)
-- print('Spatial Convolution Map Connection Table Rev')
-- print(spat_conv_map.connTableRev)
for f_out=1,num_feats_out do
  spat_conv_map.bias[{f_out}] = f_out / num_feats_out - 0.5
end
-- print('Spatial Convolution Map Biases')
-- print(spat_conv_map.bias)
num_filt = fan_in * num_feats_out
sigma_x_sq = 1
sigma_y_sq = 1
for filt=1,num_filt do
  for v=1,filt_height do
    for u=1,filt_width do
      x = u - 1 - (filt_width-1) / 2
      y = v - 1 - (filt_height-1) / 2
      spat_conv_map.weight[{filt, v, u}] = (filt / num_filt) * math.exp(-((x*x) / (2*sigma_x_sq) + 
        (y*y) / (2*sigma_y_sq)))
    end
  end
end
-- print('Spatial Convolution Map Weights')
-- print(spat_conv_map.weight)
model:add(spat_conv_map)
res = model:forward(data_in)
saveArray(res, "test_data/spatial_convolution_map.bin")
print('Spatial Convolution Map result result saved to test_data/spatial_convolution_map.bin')

-- Test SpatialConvolution
n_states_in = num_feats_in
n_states_out = num_feats_out
fan_in = n_states_in
filt_width = 5
filt_height = 5
spat_conv = nn.SpatialConvolution(n_states_in, n_states_out, filt_width, filt_height)
for f_out=1,num_feats_out do
  spat_conv.bias[{f_out}] = f_out / num_feats_out - 0.5
end
-- print('Spatial Convolution Biases')
-- print(spat_conv.bias)
num_filt = num_feats_out * num_feats_in
sigma_x_sq = 1
sigma_y_sq = 1
for fout=1,num_feats_out do
  for fin=1,num_feats_in do
    for v=1,filt_height do
      for u=1,filt_width do
        filt = (fout-1) * num_feats_out + (fin - 1) + 1
        x = u - 1 - (filt_width-1) / 2
        y = v - 1 - (filt_height-1) / 2
        spat_conv.weight[{fout, fin, v, u}] = (filt / num_filt) * math.exp(-((x*x) / (2*sigma_x_sq) + 
          (y*y) / (2*sigma_y_sq)))
      end
    end
  end
end
-- print('Spatial Convolution Weights')
-- print(spat_conv.weight)
res = spat_conv:forward(model:get(2).output)
saveArray(res, "test_data/spatial_convolution.bin")
print('Spatial Convolution result result saved to test_data/spatial_convolution.bin')

-- Test SpatialLPPooling
pnorm = 2.0
nstates = num_feats_out
poolsize_u = 2
poolsize_v = 2
pool_stage = nn.SpatialLPPooling(nstates, poolsize_u, poolsize_v, poolsize_u, poolsize_v)
model:add(pool_stage)
res = model:forward(data_in)
saveArray(res, "test_data/spatial_lp_pooling.bin")
print('Spatial LP Pooling result result saved to test_data/spatial_lp_pooling.bin')

-- Test SpatialMaxPooling
model3 = nn.Sequential()
max_pool_stage = nn.SpatialMaxPooling(poolsize_u, poolsize_v, poolsize_u, poolsize_v)
model3:add(max_pool_stage)
res = model3:forward(data_in)
saveArray(res, "test_data/spatial_max_pooling.bin")
print('Spatial Max Pooling result result saved to test_data/spatial_max_pooling.bin')

-- Test SpatialSubtractiveNormalization
model4 = nn.Sequential()
normkernel = image.gaussian1D(7)
print('Normalization Kernel1D')
print(normkernel)
norm = nn.SpatialSubtractiveNormalization(num_feats_in, normkernel)
model4:add(norm)
res = model4:forward(data_in)
saveArray(res, "test_data/spatial_subtractive_normalization.bin")
print('SpatialSubtractiveNormalization result result saved to test_data/spatial_subtractive_normalization.bin')

model4 = nn.Sequential()
normkernel = image.gaussian(7)
print('Normalization Kernel2D')
print(normkernel)
norm = nn.SpatialSubtractiveNormalization(num_feats_in, normkernel)
model4:add(norm)
res = model4:forward(data_in)
saveArray(res, "test_data/spatial_subtractive_normalization_2d.bin")
print('SpatialSubtractiveNormalization result result saved to test_data/spatial_subtractive_normalization_2d.bin')

-- Test SpatialDivisiveNormalization
model5 = nn.Sequential()
normkernel = image.gaussian1D(7)
print('Normalization Kernel1D')
print(normkernel)
spatial_div_norm = nn.SpatialDivisiveNormalization(num_feats_in, normkernel)
model5:add(spatial_div_norm)
res = model5:forward(data_in)
saveArray(res, "test_data/spatial_divisive_normalization.bin")
print('SpatialDivisiveNormalization result result saved to test_data/spatial_divisive_normalization.bin')

model5 = nn.Sequential()
normkernel = image.gaussian(7)
{'fraction_non_alphanumeric': 0.11676699716713881, 'fraction_numerical': 0.017174220963172806, 'mean_word_length': 5.339506172839506, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5804626', 'n_tokens_mistral': 4588, 'n_tokens_neox': 4459, 'n_words': 856}