-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
393 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import * as tf from "@tensorflow/tfjs" | ||
import { GaussianLikelihood } from "./gaussian-likelihood" | ||
import { MlpSpecification, mlp } from "./mlp" | ||
|
||
const LOG_STD_MIN = -20 | ||
const LOG_STD_MAX = 2 | ||
|
||
export class Actor extends tf.layers.Layer { | ||
private gaussianLikelihood: tf.layers.Layer | ||
|
||
private net: tf.Sequential | ||
private meanLayer: tf.layers.Layer | ||
private stdevLayer: tf.layers.Layer | ||
|
||
constructor(observationSize: number, actionSize: number, mlpSpec: MlpSpecification) { | ||
super() | ||
|
||
this.net = mlp({ | ||
...mlpSpec, | ||
sizes: [observationSize, ...mlpSpec.sizes], | ||
}) | ||
|
||
this.meanLayer = tf.layers.dense({ | ||
units: actionSize, | ||
}) | ||
|
||
this.stdevLayer = tf.layers.dense({ | ||
units: actionSize, | ||
}) | ||
|
||
this.gaussianLikelihood = new GaussianLikelihood() | ||
} | ||
|
||
call(x: tf.Tensor<tf.Rank>): tf.Tensor<tf.Rank>[] { | ||
x = this.net.apply(x) as tf.Tensor<tf.Rank> | ||
const mu = this.meanLayer.apply(x) as tf.Tensor<tf.Rank> | ||
|
||
let logSigma = this.stdevLayer.apply(x) as tf.Tensor<tf.Rank> | ||
logSigma = tf.clipByValue(logSigma, LOG_STD_MIN, LOG_STD_MAX) | ||
const sigma = tf.exp(logSigma) | ||
|
||
let action = tf.mul(tf.randomNormal(mu.shape), sigma) | ||
action = tf.tanh(action) | ||
|
||
let logpPi = this.gaussianLikelihood.apply([action, mu, logSigma]) as tf.Tensor<tf.Rank> | ||
|
||
logpPi = tf.sub( | ||
logpPi, | ||
tf.sum( | ||
tf.mul(2, tf.sub(tf.sub(Math.log(2), action), tf.softplus(tf.mul(-2, action)))), | ||
1, | ||
), | ||
) | ||
|
||
return [action, logpPi] | ||
} | ||
|
||
get trainableWeights(): tf.LayerVariable[] { | ||
return [ | ||
...this.net.trainableWeights, | ||
...this.meanLayer.trainableWeights, | ||
...this.stdevLayer.trainableWeights, | ||
] | ||
} | ||
|
||
static get className() { | ||
return "Actor" | ||
} | ||
} | ||
|
||
tf.serialization.registerClass(Actor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as tf from "@tensorflow/tfjs" | ||
import { MlpSpecification, mlp } from "./mlp" | ||
|
||
export class Critic extends tf.layers.Layer { | ||
private q: tf.Sequential | ||
|
||
constructor(observationSize: number, actionSize: number, mlpSpec: MlpSpecification) { | ||
super() | ||
|
||
this.q = mlp({ | ||
...mlpSpec, | ||
sizes: [observationSize + actionSize, ...mlpSpec.sizes], | ||
outputActivation: undefined, | ||
}) | ||
} | ||
|
||
call([obs, act]: tf.Tensor<tf.Rank>[]): tf.Tensor<tf.Rank> { | ||
let x = tf.concat([obs, act], 1) | ||
x = this.q.apply(x) as tf.Tensor<tf.Rank> | ||
return tf.squeeze(x, [1]) | ||
} | ||
|
||
get trainableWeights(): tf.LayerVariable[] { | ||
return this.q.trainableWeights | ||
} | ||
|
||
static get className() { | ||
return "Critic" | ||
} | ||
} | ||
|
||
tf.serialization.registerClass(Critic) |
25 changes: 25 additions & 0 deletions
25
packages/learning/src/soft-actor-critic/gaussian-likelihood.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as tf from "@tensorflow/tfjs" | ||
|
||
export class GaussianLikelihood extends tf.layers.Layer { | ||
computeOutputShape(inputShape: tf.Shape[]): tf.Shape | tf.Shape[] { | ||
return [inputShape[0][0], 1] | ||
} | ||
|
||
call([x, mu, logstd]: tf.Tensor<tf.Rank>[]): tf.Tensor<tf.Rank> { | ||
const preSum = tf.mul( | ||
-0.5, | ||
tf.add( | ||
tf.pow(tf.div(tf.sub(x, mu), tf.exp(logstd)), 2), | ||
tf.add(tf.mul(2, logstd), Math.log(2 * Math.PI)), | ||
), | ||
) | ||
|
||
return tf.sum(preSum, 1) | ||
} | ||
|
||
static get className() { | ||
return "GaussianLikelihood" | ||
} | ||
} | ||
|
||
tf.serialization.registerClass(GaussianLikelihood) |
Oops, something went wrong.