Skip to content

Commit

Permalink
some test fixes, start of tiny-world task
Browse files Browse the repository at this point in the history
  • Loading branch information
iislucas committed Jun 11, 2024
1 parent eb2caa7 commit dd76295
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const layerNormTrainer = new TrainerMetaData('transformer', {
name: 'layerNormTrainer',
trainState: {
batchSize: 64,
learningRate: 30,
learningRate: 10,
maxInputlength: 5,
testSetSize: 64,
trainSetSize: 64 * 10000,
Expand All @@ -122,7 +122,7 @@ const noLayerNormTrainer = new TrainerMetaData('transformer', {
name: 'noLayerNormTrainer',
trainState: {
batchSize: 64,
learningRate: 1,
learningRate: 0.5,
maxInputlength: 5,
testSetSize: 64,
trainSetSize: 64 * 10000,
Expand Down Expand Up @@ -392,6 +392,7 @@ export class ModelTaskTrainerComponent {
currentModelData.params,
trainer.config.trainState
);

trainer.trainState.set(newState);
this.curMetrics = computeMetrics(newState);
this.addMetricsToGraph(this.curMetrics, trainer.config.name);
Expand Down
20 changes: 12 additions & 8 deletions animated-transformer/src/app/web-colab/app.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ const onceInputs = new Promise<string>((resolve) => {
});
});

const inputs = await onceInputs;
async function run() {
const inputs = await onceInputs;

console.log(inputs);
console.log(inputs);

const t = new GTensor(tf.tensor([1, 2, 3]), ['a']);
const v = t.contract(t, ['a']).tensor.arraySync() as number;
const t = new GTensor(tf.tensor([1, 2, 3]), ['a']);
const v = t.contract(t, ['a']).tensor.arraySync() as number;

postMessage({
t,
v,
});
postMessage({
t,
v,
});
}

run();
229 changes: 229 additions & 0 deletions animated-transformer/src/lib/seqtasks/tiny_worlds.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/* Copyright 2023 Google LLC. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

import { TinyWorldTask, TinyWorldTaskConfig, sepToken } from './tiny_worlds';
import { Example } from './util';

const exampleRelations = ['is', 'jumps-over', 'runs-away', 'squishes'];
type ExampleRelations = (typeof exampleRelations)[number];

// TODO: can we skip individual instances, e.g. c0, c1, etc, and just have the kinds?
// (instances being defined in the context?)
const exampleObjects = [
// 'c0:cat:animal',
// 'c1:cat:animal',
// 'c2:cat:animal',
// 'm0:monkey:animal',
// 'm1:monkey:animal',
// 'm2:monkey:animal',
// 'e0:elephant:animal',
// 'e1:elephant:animal',
// 'e2:elephant:animal',
'animal',
'cat.animal',
'monkey.animal',
'elephant.animal',
// 't0:tree:thing',
// 't1:tree:thing',
// 't2:tree:thing',
// 'r0:rock:thing',
// 'r1:rock:thing',
// 'r2:rock:thing',
// 'f0:flower:thing',
// 'f1:flower:thing',
// 'f2:flower:thing',
'thing',
'flower.thing',
'rock.thing',
'tree.thing',
'', // the type of all objects, everything ends with an empty string;
] as const;

type ExampleObjects = (typeof exampleObjects)[number];

const relArgs = new Map<ExampleRelations, ExampleObjects[]>([
['is', ['', '']],
['jumps-over', ['animal', '']],
['runs-away', ['animal']],
['squishes', ['animal', '']],
]);

const rules = [
// Monkeys jump over stuff a lot
'jumps-over _x:monkey _y += 5',
// If a monkey just jumped over something, they are not so likely to jump again right away.
// '[... jumps-over _x _y ...:3] ==> jumps-over _x _y *= 0.1',
// Monkeys might squish flowers
'jumps-over _x:monkey _y:flower ==> squishes _x _y += 1',
// Monkeys might squish cats
'jumps-over _x:monkey _y:cat ==> squishes _x _y += 0.5',
// cats jump over stuff
'jumps-over _x:cat _y += 1',
// cats very occationally squish flowers
'jumps-over _x:cat _y:flower ==> squishes _x _y += 0.1',
// Elephants occationally jump over animals
'jumps-over _x:elephant _y:animal += 0.1',
// Cats sometimes run away when jumped over
'jumps-over _x _y:cat ==> runs-away _y += 1',
// Squished animals can't run away anymore
'squishes _x _y:animal ==> runs-away _y = 0',
// Animals that can away can't get squished or be jumped over.
'runs-away _y:animal ==> squishes _x _y = 0',
'runs-away _y:animal ==> jumps-over _x _y = 0',
];

const impactRegexp = new RegExp(
/\s*(?<rel>\S*)\s*(?<op>(\+\=|\=))\s*(?<val>\S*)\s*/
);
type ImpactMatches = { rel: string; op: '+=' | '='; val: string };

const relRegexp = new RegExp(/\s*(?<relName>\S*)\s+(?<args>(\_\S*\s+)*)/);
type RelMatch = { relName: string; args: string[] };

function parseRel(rel: string) {
const matches = rel.match(relRegexp);
return matches;
}

function parseRule(rule: string) {
const conditionsAndConclusion = rule.split(/\s*\=\=\>\s*/);
let conditions = undefined;
let conclusion = undefined;
if (conditionsAndConclusion.length > 1) {
[conditions, conclusion] = conditionsAndConclusion;
} else {
conclusion = conditionsAndConclusion[0];
}
const { rel, op, val } = conclusion.match(impactRegexp)!
.groups as ImpactMatches;
}

function ruleParser(rules: string[], context: string) {
const observations = context.split(sepToken);
}

// ('is _x monkey => jumps-over _x _y += 1');
// ('is _x monkey => jumps-over _x rock += 1');
// ('is _x monkey => jumps-over _x flower += 1');
// ('is _x monkey => jumps-over _x tree += 0.1');

// class RVar {
// constructor(name: string) {}
// }

// const rules = [
// // some probability is introducing a new object
// {
// context: {
// notPresent: [
// {
// relation: 'is',
// arguments: [{ varbind: 'X' }, { varbind: 'Y' }],
// },
// ],
// present: [],
// },
// add: {
// relation: 'is',
// arguments: [
// { kind: 'newObjectName', varbind: 'X' },
// { kind: 'object', objPostfixMatch: '', varbind: 'Y' },
// ],
// },
// scoreMerge: { addToBaseScore: 1 },
// },
// // small probability of repeating an observation
// {
// context: {
// notPresent: [],
// present: [
// {
// relation: 'is',
// arguments: [{ varbind: 'X' }, { varbind: 'Y' }],
// },
// ],
// },
// add: {
// relation: 'is',
// arguments: [
// { kind: 'object', objPostfixMatch: '', varbind: 'X' },
// { kind: 'object', objPostfixMatch: '', varbind: 'Y' },
// ],
// },
// scoreMerge: { addToBaseScore: 0.1 },
// },
// ];

// const example_TinyWorldTaskConfig: TinyWorldTaskConfig<
// ExampleObjects,
// ExampleRelations
// > = {
// name: 'example-tiny-world',
// maxInputLen: 20,
// maxOutputLen: 10,
// seed: 0,
// objectTokens: [...exampleObjects],
// relationTokenArgs: relArgs,
// };

describe('tiny_world_task', () => {
beforeEach(() => {});

it('genRandExample: DecisionBoundaryTask', () => {
const rel = parseRel('squishes _x _y:animal');
console.log(rel);
expect(rel).not.toBeNull();

// const example_TinyWorldTaskConfig: TinyWorldTaskConfig<
// ExampleObjects,
// ExampleRelations
// > = {
// name: 'example-tiny-world',
// maxInputLen: 20,
// maxOutputLen: 10,
// seed: 0,
// objectTokens: [...exampleObjects],
// relationTokenArgs: relArgs,
// };
// const task = new TinyWorldTask(example_TinyWorldTaskConfig);

// let example: Example;
// example = task.genRandExample();

// expect(example.secret).toEqual(['2']);
// expect(example.input).toEqual(['5', 'F', '4', 'F', '3']);
// expect(example.output).toEqual(['F']);

// example = task.genRandExample();
// expect(example.secret).toEqual(['2']);
// expect(example.input).toEqual(['3', 'F', '2', 'T', '1']);
// expect(example.output).toEqual(['T']);

// example = task.genRandExample();
// expect(example.secret).toEqual(['3']);
// expect(example.input).toEqual(['3', 'T', '3', 'T', '4']);
// expect(example.output).toEqual(['F']);

// example = task.genRandExample();
// expect(example.secret).toEqual(['4']);
// expect(example.input).toEqual(['1', 'T', '3', 'T', '4']);
// expect(example.output).toEqual(['T']);

// example = task.genRandExample();
// expect(example.secret).toEqual(['3']);
// expect(example.input).toEqual(['1', 'T', '4', 'F', '5']);
// expect(example.output).toEqual(['F']);
});
});
Loading

0 comments on commit dd76295

Please sign in to comment.