Commit ebc0296e authored by Christian Fuß's avatar Christian Fuß
Browse files

added Repeat and Dot Layer

parent 09e42ded
Pipeline #186553 failed with stages
in 2 minutes and 16 seconds
...@@ -354,8 +354,8 @@ public class CNNArch2GluonTemplateController extends CNNArchTemplateController { ...@@ -354,8 +354,8 @@ public class CNNArch2GluonTemplateController extends CNNArchTemplateController {
return unroll.getIntValue(AllPredefinedLayers.WIDTH_NAME).get(); return unroll.getIntValue(AllPredefinedLayers.WIDTH_NAME).get();
} }
public int getBeamSearchDepth(UnrollInstructionSymbol unroll){ public int getBeamSearchLength(UnrollInstructionSymbol unroll){
return unroll.getIntValue(AllPredefinedLayers.DEPTH_NAME).get(); return unroll.getIntValue(AllPredefinedLayers.MAX_LENGTH_NAME).get();
} }
} }
...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss): ...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss):
return F.mean(loss, axis=self._batch_axis, exclude=True) return F.mean(loss, axis=self._batch_axis, exclude=True)
class ${tc.fileNameWithoutEnding}: class ${tc.fileNameWithoutEnding}:
def applyBeamSearch(input, depth, width, maxDepth, currProb, netIndex, bestOutput): def applyBeamSearch(input, length, width, maxLength, currProb, netIndex, bestOutput):
bestProb = 0.0 bestProb = 0.0
while depth < maxDepth: while length < maxLength:
depth += 1 length += 1
batchIndex = 0 batchIndex = 0
for batchEntry in input: for batchEntry in input:
top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width) top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width)
...@@ -44,15 +44,15 @@ class ${tc.fileNameWithoutEnding}: ...@@ -44,15 +44,15 @@ class ${tc.fileNameWithoutEnding}:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
#print top_k_values[index] #print top_k_values[index]
if depth == 1: if length == 1:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index]))) currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index])))
else: else:
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, bestOutput) currProb * top_k_values[index], netIndex, bestOutput)
if depth == maxDepth: if length == maxLength:
#print currProb #print currProb
if currProb > bestProb: if currProb > bestProb:
bestProb = currProb bestProb = currProb
...@@ -226,6 +226,9 @@ class ${tc.fileNameWithoutEnding}: ...@@ -226,6 +226,9 @@ class ${tc.fileNameWithoutEnding}:
else: else:
predictions.append(output_name) predictions.append(output_name)
#print [word[0] for word in predictions]
#print labels[0]
metric.update(preds=predictions, labels=labels) metric.update(preds=predictions, labels=labels)
train_metric_score = metric.get()[1] train_metric_score = metric.get()[1]
......
<#assign dim = element.dim?c>
<#if mode == "ARCHITECTURE_DEFINITION"> <#if mode == "ARCHITECTURE_DEFINITION">
self.${element.name} = Concatenate(dim=1) self.${element.name} = Concatenate(dim=${dim})
<#include "OutputShape.ftl"> <#include "OutputShape.ftl">
<#elseif mode == "FORWARD_FUNCTION"> <#elseif mode == "FORWARD_FUNCTION">
${element.name} = self.${element.name}(${tc.join(element.inputs, ", ")}) ${element.name} = self.${element.name}(${tc.join(element.inputs, ", ")})
......
<#if mode == "ARCHITECTURE_DEFINITION">
self.${element.name} = mx.nd.sparse.dot(${tc.join(element.inputs, ",")})
<#include "OutputShape.ftl">
<#elseif mode == "FORWARD_FUNCTION">
${element.name} = self.${element.name}(${tc.join(element.inputs, ",")})
</#if>
\ No newline at end of file
<#assign repeats = element.repeats?c>
<#assign axis = element.axis?c>
<#if mode == "ARCHITECTURE_DEFINITION">
self.${element.name} = mx.nd.repeat(repeats=${repeats}, axis=${axis})
<#include "OutputShape.ftl">
<#elseif mode == "FORWARD_FUNCTION">
${element.name} = self.${element.name}(${input})
</#if>
\ No newline at end of file
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
<#list networkInstruction.toUnrollInstruction().resolvedBodies as resolvedBody> <#list networkInstruction.toUnrollInstruction().resolvedBodies as resolvedBody>
<#if networkInstruction.name == "BeamSearch"> <#if networkInstruction.name == "BeamSearch">
input = ${tc.join(tc.getStreamInputNames(networkInstruction.body, resolvedBody), ", ")} input = ${tc.join(tc.getStreamInputNames(networkInstruction.body, resolvedBody), ", ")}
<#assign depth = tc.getBeamSearchDepth(networkInstruction.toUnrollInstruction())> <#assign length = tc.getBeamSearchLength(networkInstruction.toUnrollInstruction())>
<#assign width = tc.getBeamSearchWidth(networkInstruction.toUnrollInstruction())> <#assign width = tc.getBeamSearchWidth(networkInstruction.toUnrollInstruction())>
${tc.getStreamOutputNames(networkInstruction.body, resolvedBody)[0]} = applyBeamSearch(input, 0, ${depth}, ${width}, 1.0, ${networkInstruction?index}, input) ${tc.getStreamOutputNames(networkInstruction.body, resolvedBody)[0]} = applyBeamSearch(input, 0, ${length}, ${width}, 1.0, ${networkInstruction?index}, input)
<#else> <#else>
${tc.join(tc.getStreamOutputNames(networkInstruction.body, resolvedBody), ", ")} = self._networks[${networkInstruction?index}](${tc.join(tc.getStreamInputNames(networkInstruction.body, resolvedBody), ", ")}) ${tc.join(tc.getStreamOutputNames(networkInstruction.body, resolvedBody), ", ")} = self._networks[${networkInstruction?index}](${tc.join(tc.getStreamInputNames(networkInstruction.body, resolvedBody), ", ")})
<#if !(tc.getStreamOutputNames(networkInstruction.body)[0]?ends_with("_output_"))> <#if !(tc.getStreamOutputNames(networkInstruction.body)[0]?ends_with("_output_"))>
......
...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss): ...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss):
return F.mean(loss, axis=self._batch_axis, exclude=True) return F.mean(loss, axis=self._batch_axis, exclude=True)
class CNNSupervisedTrainer_Alexnet: class CNNSupervisedTrainer_Alexnet:
def applyBeamSearch(input, depth, width, maxDepth, currProb, netIndex, bestOutput): def applyBeamSearch(input, length, width, maxLength, currProb, netIndex, bestOutput):
bestProb = 0.0 bestProb = 0.0
while depth < maxDepth: while length < maxLength:
depth += 1 length += 1
batchIndex = 0 batchIndex = 0
for batchEntry in input: for batchEntry in input:
top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width) top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width)
...@@ -44,15 +44,15 @@ class CNNSupervisedTrainer_Alexnet: ...@@ -44,15 +44,15 @@ class CNNSupervisedTrainer_Alexnet:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
#print top_k_values[index] #print top_k_values[index]
if depth == 1: if length == 1:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index]))) currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index])))
else: else:
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, bestOutput) currProb * top_k_values[index], netIndex, bestOutput)
if depth == maxDepth: if length == maxLength:
#print currProb #print currProb
if currProb > bestProb: if currProb > bestProb:
bestProb = currProb bestProb = currProb
...@@ -224,6 +224,9 @@ class CNNSupervisedTrainer_Alexnet: ...@@ -224,6 +224,9 @@ class CNNSupervisedTrainer_Alexnet:
else: else:
predictions.append(output_name) predictions.append(output_name)
#print [word[0] for word in predictions]
#print labels[0]
metric.update(preds=predictions, labels=labels) metric.update(preds=predictions, labels=labels)
train_metric_score = metric.get()[1] train_metric_score = metric.get()[1]
......
...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss): ...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss):
return F.mean(loss, axis=self._batch_axis, exclude=True) return F.mean(loss, axis=self._batch_axis, exclude=True)
class CNNSupervisedTrainer_CifarClassifierNetwork: class CNNSupervisedTrainer_CifarClassifierNetwork:
def applyBeamSearch(input, depth, width, maxDepth, currProb, netIndex, bestOutput): def applyBeamSearch(input, length, width, maxLength, currProb, netIndex, bestOutput):
bestProb = 0.0 bestProb = 0.0
while depth < maxDepth: while length < maxLength:
depth += 1 length += 1
batchIndex = 0 batchIndex = 0
for batchEntry in input: for batchEntry in input:
top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width) top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width)
...@@ -44,15 +44,15 @@ class CNNSupervisedTrainer_CifarClassifierNetwork: ...@@ -44,15 +44,15 @@ class CNNSupervisedTrainer_CifarClassifierNetwork:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
#print top_k_values[index] #print top_k_values[index]
if depth == 1: if length == 1:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index]))) currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index])))
else: else:
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, bestOutput) currProb * top_k_values[index], netIndex, bestOutput)
if depth == maxDepth: if length == maxLength:
#print currProb #print currProb
if currProb > bestProb: if currProb > bestProb:
bestProb = currProb bestProb = currProb
...@@ -224,6 +224,9 @@ class CNNSupervisedTrainer_CifarClassifierNetwork: ...@@ -224,6 +224,9 @@ class CNNSupervisedTrainer_CifarClassifierNetwork:
else: else:
predictions.append(output_name) predictions.append(output_name)
#print [word[0] for word in predictions]
#print labels[0]
metric.update(preds=predictions, labels=labels) metric.update(preds=predictions, labels=labels)
train_metric_score = metric.get()[1] train_metric_score = metric.get()[1]
......
...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss): ...@@ -32,10 +32,10 @@ class LogCoshLoss(gluon.loss.Loss):
return F.mean(loss, axis=self._batch_axis, exclude=True) return F.mean(loss, axis=self._batch_axis, exclude=True)
class CNNSupervisedTrainer_VGG16: class CNNSupervisedTrainer_VGG16:
def applyBeamSearch(input, depth, width, maxDepth, currProb, netIndex, bestOutput): def applyBeamSearch(input, length, width, maxLength, currProb, netIndex, bestOutput):
bestProb = 0.0 bestProb = 0.0
while depth < maxDepth: while length < maxLength:
depth += 1 length += 1
batchIndex = 0 batchIndex = 0
for batchEntry in input: for batchEntry in input:
top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width) top_k_indices = mx.nd.topk(batchEntry, axis=0, k=width)
...@@ -44,15 +44,15 @@ class CNNSupervisedTrainer_VGG16: ...@@ -44,15 +44,15 @@ class CNNSupervisedTrainer_VGG16:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
#print top_k_values[index] #print top_k_values[index]
if depth == 1: if length == 1:
#print mx.nd.array(top_k_indices[index]) #print mx.nd.array(top_k_indices[index])
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index]))) currProb * top_k_values[index], netIndex, self._networks[netIndex](mx.nd.array(top_k_indices[index])))
else: else:
result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), depth, width, maxDepth, result = applyBeamSearch(self._networks[netIndex](mx.nd.array(top_k_indices[index])), length, width, maxLength,
currProb * top_k_values[index], netIndex, bestOutput) currProb * top_k_values[index], netIndex, bestOutput)
if depth == maxDepth: if length == maxLength:
#print currProb #print currProb
if currProb > bestProb: if currProb > bestProb:
bestProb = currProb bestProb = currProb
...@@ -224,6 +224,9 @@ class CNNSupervisedTrainer_VGG16: ...@@ -224,6 +224,9 @@ class CNNSupervisedTrainer_VGG16:
else: else:
predictions.append(output_name) predictions.append(output_name)
#print [word[0] for word in predictions]
#print labels[0]
metric.update(preds=predictions, labels=labels) metric.update(preds=predictions, labels=labels)
train_metric_score = metric.get()[1] train_metric_score = metric.get()[1]
......
...@@ -14,7 +14,7 @@ architecture RNNencdec(max_length=50, vocabulary_size=30000, hidden_size=1000){ ...@@ -14,7 +14,7 @@ architecture RNNencdec(max_length=50, vocabulary_size=30000, hidden_size=1000){
encoder.state -> decoder.state; encoder.state -> decoder.state;
timed<t> BeamSearch(max_length=50, depth=2, width=2) { timed<t> BeamSearch(max_length=50, width=2) {
target[t-1] -> target[t-1] ->
Embedding(output_dim=hidden_size) -> Embedding(output_dim=hidden_size) ->
decoder -> decoder ->
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment