I'm trying to figure out how to implement DDPG in Tensorflow.js using Python examples such as this one from keras website. I got stuck on the training code:
with tf.GradientTape() as tape:
target_actions = target_actor(next_state_batch, training=True)
y = reward_batch + gamma * target_critic(
[next_state_batch, target_actions], training=True
)
critic_value = critic_model([state_batch, action_batch], training=True)
critic_loss = tf.math.reduce_mean(tf.math.square(y - critic_value))
critic_grad = tape.gradient(critic_loss, critic_model.trainable_variables)
critic_optimizer.apply_gradients(
zip(critic_grad, critic_model.trainable_variables)
)
with tf.GradientTape() as tape:
actions = actor_model(state_batch, training=True)
critic_value = critic_model([state_batch, actions], training=True)
# Used `-value` as we want to maximize the value given
# by the critic for our actions
actor_loss = -tf.math.reduce_mean(critic_value)
actor_grad = tape.gradient(actor_loss, actor_model.trainable_variables)
actor_optimizer.apply_gradients(
zip(actor_grad, actor_model.trainable_variables)
)
So far my typescript version looks like this:
const batch = this.memory.getMinibatch(this.config.replayBatchSize);
const states = this.actorService.getStateTensor(this.actor, ...batch.map(s => s.state));
const nextStates = this.actorService.getStateTensor(this.actor, ...batch.map(s => s.nextState));
const rewards = tf.tensor2d(batch.map(s => s.reward), [batch.length, 1], 'float32');
const actions = this.actorService.getActionTensor(...batch.map(s => s.action));
const criticLossFunction = () => tf.tidy(() => {
let targetQs: tf.Tensor;
if (this.config.discountRate === 0) {
targetQs = rewards;
} else {
const targetActions = this.targetActorModel.predict(nextStates) as tf.Tensor;
const targetCriticQs = this.targetCriticModel.predict(tf.concat([nextStates, targetActions], 1)) as tf.Tensor;
targetQs = rewards.add(targetCriticQs.mul(this.config.discountRate));
}
const criticQs = this.criticModel.predict(tf.concat([states, actions], 1)) as tf.Tensor;
const criticLoss = tf.losses.meanSquaredError(targetQs, criticQs);
return criticLoss.asScalar();
});
const criticTrainableVars = this.criticModel.getWeights(true) as tf.Variable<tf.Rank>[];
const criticGradient = tf.variableGrads(criticLossFunction, criticTrainableVars);
// HOWTO: zip(critic_grad, critic_model.trainable_variables)
this.criticModel.optimizer.applyGradients(criticGradient.grads);
tf.dispose(criticGradient);
const actorLossFunction = () => tf.tidy(() => {
const policyActions = this.actorModel.predict(states) as tf.Tensor;
const criticQs = this.criticModel.predict(tf.concat([states, policyActions], 1)) as tf.Tensor;
const actorLoss = tf.mean(criticQs.mul(-1));
return actorLoss.asScalar()
});
const actorTrainableVars = this.actorModel.getWeights(true) as tf.Variable<tf.Rank>[];
const actorGradient = tf.variableGrads(actorLossFunction, actorTrainableVars);
// HOWTO: zip(actor_grad, actor_model.trainable_variables)
this.actorModel.optimizer.applyGradients(actorGradient.grads);
const actorLoss = actorGradient.value.dataSync()[0];
tf.dispose(actorGradient);
but my code does not work correctly (the loss is too high on a very simple task) due to it's missing one major step (?): zipping critic grads with critic trainable vars before passing it to applyGradients.
False alarm, the code works correctly, no need to zip or anything. High loss was due to I used actorLoss as the value instead of criticLoss (criticGradient.value.dataSync()[0])
Related
I have a method in viewmodel that I want to execute infinity till client stop that.
This loop should work for each button separately and stop that too.
But when I execute the loop for fourth time, application hangs.
How can I manage the loop and run it for four separate objects
This is my method in viewmodel:
fun getLocationInfinity(context: Context, tripId: Long, passengerId: Int) =
viewModelScope.launch {
val gpsTracker = LocationGpsTracker(context, 0, 0)
val netGpsTracker = LocationGpsTrackerNetwork(context)
var way = Way()
way.latitude1 = gpsTracker.getLatitude()
way.longitude1 = gpsTracker.getLongitude()
way.accuracy1 = gpsTracker.getAccuracy()
way.latitudeNet1 = netGpsTracker.getLatitude()
way.longitudeNet1 = netGpsTracker.getLongitude()
way.accuracyNet1 = netGpsTracker.getAccuracy()
while (isActive) {
if (_passengerSwitch.value?.get(passengerId - 1) == true) {
way.latitude2 = way.latitude1
way.longitude2 = way.longitude1
way.accuracy2 = way.accuracy1
way.latitudeNet2 = way.latitudeNet1
way.longitudeNet2 = way.longitudeNet1
way.accuracyNet2 = way.accuracyNet1
way.latitude1 = gpsTracker.getLatitude()
way.longitude1 = gpsTracker.getLongitude()
way.accuracy1 = gpsTracker.getAccuracy()
way.latitudeNet1 = netGpsTracker.getLatitude()
way.longitudeNet1 = netGpsTracker.getLongitude()
way.accuracyNet1 = netGpsTracker.getAccuracy()
_way.postValue(way)
val tripDetails = TripDetails(
latitude1 = way.latitude1,
latitude2 = way.latitude2,
longitude1 = way.longitude1,
longitude2 = way.longitude2,
accuracy1 = way.accuracy1,
accuracy2 = way.accuracy2,
latitudeNet1 = way.latitudeNet1,
latitudeNet2 = way.latitudeNet2,
longitudeNet1 = way.longitudeNet1,
longitudeNet2 = way.longitudeNet2,
accuracy1Net = way.accuracyNet1,
accuracy2Net = way.accuracyNet2,
distance = null,
isCalculated = false,
tripId = tripId.toInt(),
isEnded = false
)
localRepository.insertLocation(tripDetails)
delay(2000)
}
}
}
The delay() call needs to be outside your if-block. Otherwise, if the condition is false, this loop will never suspend so it never relinquishes the thread.
If send to Avax but not my Erc20 Token. Thank you for your help
First we get the url of the rcp.
Then we create an instance of web3.js.
Then with our private key you create an account.
We create an instance of our contract by passing it the abi and the address of the contract as parameters.
We estimate the gas with estimateGas passing it an object that indicates the function of the abi that is going to be used, the address of the contract, to whom it is going to be sent.
We create a transfer object
We obtain the balance to know the initial balance
We sign the transaction with our private key
We send the transaction
We get the bottom line
My code
`
const transferToeknErc20 = async () => {
const amount = '1000000000000000';
const jsonInterface = [{"inputs":[],"stateMutability":....
const contractAddress = '0x7B9...';
const privateKeyWallet = '14f...';
const chainId = 43113;
const address_to = '0x86...';
//NODE
const NODE_URL = "https://api.avax-test.network/ext/bc/C/rpc";
//WEB3
const web3Global = new Web3( new Web3.providers.HttpProvider(NODE_URL));
//Creamos una cuenta con la llave privada
const account = web3Global.eth.accounts.privateKeyToAccount(privateKeyWallet);
//CONTRACT
const contract = new web3Global.eth.Contract(jsonInterface, contractAddress);
//////////////////////////////////////////////////////////////////////////////
let estimateGas = await web3Global.eth.estimateGas({
value: '0x0', // Only tokens
data: contract.methods.transfer(address_to, amount).encodeABI(),
from: account.address,
to: address_to
});
//////////////////////////////////////////////////////////////////////////////
const transactionObject = {
value:'0x0',
data:contract.methods.transfer(address_to, amount).encodeABI(),
from: account.address,
to: address_to,
gas:web3Global.utils.toHex(Math.round(estimateGas * 1.10)),
gasLimit:web3Global.utils.toHex(Math.round(estimateGas * 1.10)),
chainId,
}
//get balanace
let balance = await contract.methods.balanceOf(account.address).call();
console.log('balance init', balance)
//Sing
const signText = await web3Global.eth.accounts.signTransaction(transactionObject, privateKeyWallet);
//Send Transaction
const reciep = await web3Global.eth.sendSignedTransaction(signText.rawTransaction);
//get balanace
balance = await contract.methods.balanceOf(account.address).call();
console.log('balance end', balance)
return null;
///////////////////////////////////////////////////////////////////////////////////////
}
`
When I get a response in Postman, I want to be able to set an environment variable (using
pm.environment.set("variable_key", "variable_value");
when a condition is met from the response array.
So the requirements:
set an environment variable named idForAbc to the value of someArray.id where someArray.criteria = "ABC" (so idForAbc = 1)
set an environment variable named idForXyz to the value of someArray.id where someArray.criteria = "XYZ" (so idForXyz = 2)
Here is my sample response:
You can choose one of 2 ways:
const res = pm.response.json();
const abc = res.someArray.find(e => e.criteria === "ABC");
if(abc){
pm.environment.set(`idFor${abc.criteria}`,abc.id)
}
const xyz = res.someArray.find(e => e.criteria === "XYZ");
if(xyz){
pm.environment.set(`idFor${xyz.criteria}`,xyz.id)
}
or
const res = pm.response.json();
function saveVarByCriteria(arr, value){
const obj = arr.find(e => e.criteria === value);
if(obj){
pm.environment.set(`idFor${obj.criteria}`, obj.id)
}
}
saveVarByCriteria(res.someArray, "ABC");
saveVarByCriteria(res.someArray, "XYZ");
I am making a discord economy/currency bot, and this is the leaderboard command. It works, but whenever I run the command !leaderboard, I don't get any of the user's tags, I just get the undefined#0000. I would like my leaderboard command to show the users with the highest amount of currency.
const { MessageEmbed } = require('discord.js');
const db = require('quick.db');
module.exports = {
name: "leaderboard",
description: 'server\'s $ leaderboard',
aliases: ['lb'],
}
module.exports.run = async (message) => {
let money = db.all().filter(data => data.ID.startsWith(`money_`)).sort((a, b) => b.data - a.data);
if (!money.length) {
let noEmbed = new MessageEmbed()
.setAuthor(message.member.displayName, message.author.displayAvatarURL())
.setColor("BLUE")
.setFooter("No leaderboard")
return message.channel.send(noEmbed)
};
money.length = 10;
var finalLb = "";
for (var i in money) {
let currency1;
let fetched = await db.fetch(`currency_${message.guild.id}`);
if (fetched == null) {
currency1 = '🎱'
} else {
currency1 = fetched
}
if (money[i].data === null) money[i].data = 0
finalLb += `**${money.indexOf(money[i]) + 1}. ${message.guild.members.cache.get(money[i].ID.split('_')[1]) ? message.guild.members.cache.get(money[i].ID.split('_')[1]).tag : "undefined#0000"}** - ${money[i].data} ${currency1}\n`;
};
const embed = new MessageEmbed()
.setTitle(message.guild.name)
.setColor("BLUE")
.setDescription(finalLb)
.setTimestamp()
.setFooter('Command: !help for currency commands')
message.channel.send(embed);
}
Try following code:
let money = db.all().filter(data => data.ID.startsWith(`money_${message.guild.id}`)).sort((a, b) => b.data - a.data)
money.length = 10;
var finalLb = "";
for (var i in money) {
finalLb += `**${money.indexOf(money[i])+1}. ${client.users.cache.get(money[i].ID.split('_')[1]) ? client.users.cache.get(money[i].ID.split('_')[1]).tag : "Unknown User#0000"}** - ${money[i].data}\n`;
}
const embed = new Discord.MessageEmbed()
.setAuthor(`Global Coin Leaderboard!`, message.guild.iconURL())
.setColor("#7289da")
.setDescription(finalLb)
.setFooter(client.user.tag, client.user.displayAvatarURL())
.setTimestamp()
message.channel.send(embed);
I personally use above code for my bot and it works pretty well for me.
Try putting the client.login('token') at the bottom of your code. Maybe the bot can't find the user tag's because of that?
After the TYPO3 (6.1.7) website of a customer has gone online, the Formhandler forms do not work correctly anymore. They do send an email, but it seems that they do not execute the Finisher_DB for writing into the database anymore.
The TypoScript settings look like this:
plugin.Tx_Formhandler.settings {
debug = 0
# GENERAL CONFIGURATION
name = Default
addErrorAnchors = 1
formValuesPrefix = formhandler
fillValueMarkersBeforeLangMarkers = 1
# ERRORS LAYOUT
singleErrorTemplate {
totalWrap = <div>|</div>
singleWrap = <span class="error">|</span><br />
}
errorListTemplate {
totalWrap = <ul>|</ul>
singleWrap = <li class="error">|</li>
}
validators {
1.class = Tx_Formhandler_Validator_Default
1.config {
fieldConf {
wish.errorCheck.1 = required
alternative.errorCheck.1 = required
firstname.errorCheck.1 = required
surname.errorCheck.1 = required
nationality.errorCheck.1 = required
dateofbirth.errorCheck.1 = required
phone.errorCheck.1 = required
email.errorCheck.1 = required
street.errorCheck.1 = required
zip.errorCheck.1 = required
city.errorCheck.1 = required
country.errorCheck.1 = required
}
}
}
# Finishers configuration
finishers {
1.class = Tx_Formhandler_Finisher_Mail
1.config {
checkBinaryCrLf = registrationMessagePlain, registrationMessageHtml
limitMailsToUser = 10
admin {
}
user {
}
}
2.class = Tx_Formhandler_Finisher_DB
2.config{
table = tx_chilifhregistration
key = uid
fields {
timeslot = Sommerplatz
timeslot_july.mapping = timeslotSummerJuly
timeslot_august.mapping = timeslotSummerAugust
timeslot_september.mapping = timeslotSummerSeptember
wish.mapping = wish
wishcategory11.mapping = wishCategory11
wishcategory19.mapping = wishCategory19
wishcategory22.mapping = wishCategory22
wishcategorydb.mapping = wishCategoryDb
alternative.mapping = alternative
alternativecategory11.mapping = alternativeCategory11
alternativecategory19.mapping = alternativeCategory19
alternativecategory22.mapping = alternativeCategory22
alternativecategorydb.mapping = alternativeCategoryDb
salutation.mapping = salutation
firstname.mapping = firstname
surname.mapping = surname
nationality.mapping = nationality
dateofbirth.mapping = dateofbirth
phone.mapping = phone
email.mapping = email
street.mapping = street
zip.mapping = zip
city.mapping = city
country.mapping = country
salutation2.mapping = salutation2
firstname2.mapping = firstname2
surname2.mapping = surname2
nationality2.mapping = nationality2
dateofbirth2.mapping = dateofbirth2
phone2.mapping = phone2
email2.mapping = email2
street2.mapping = street2
zip2.mapping = zip2
city2.mapping = city2
country2.mapping = country2
}
}
}
}
What could be the problem?
You should better use the predef-definition-style for forms. This will save you trouble with multiple forms and is a cleaner implementation.
plugin.Tx_Formhandler.settings.predef.yourformularname { ...config... }
You can find a bunch of examples on the offical site/examples
I assume that your admin and user mail-config is only empty because you won´t post any customerinformation?
Did the form work if you fill in every single field?
In my own usecases all field i map with the finisher are required, maybe you should set a ...IfEmpty-option for non-require fields.
Here are the available ifEmpty-options.