我正在浏览 Pytorch 和 Tensorflow 中的交叉熵文档。据我所知,他们正在修改交叉熵的简单实现,以解决潜在的数字上溢/下溢问题。然而,我根本无法理解这些修改有何帮助。
Pytorch 中交叉熵的实现遵循以下逻辑 -
现在,我们将它与 Tensorflow 的实现进行对比(我从 Github 的讨论中得到它。这可能是完全错误的) -
让
是所有 k 个原始 Logit 分数的向量。
虽然这解决了溢出问题,但它遇到了下溢问题,因为有可能
这将导致更小的
有人可以帮助我理解这里发生了什么吗?
约翰尼需要为他的物理课项目制作一个矩形框.他买了P厘米的电线和S cm2的特殊纸.他想用所有的线(用于12个边缘)和纸张(用于6个边)来制作盒子.
Johnny可以制作的最大音量是多少?
输入
第一行包含t,测试用例数(约10).然后是t测试案例.每个测试用例在一行中包含两个整数P和S(1≤P≤40000,1≤S≤20000).您可以假设对于给定的输入案例始终存在最佳解决方案.
产量
对于每个测试用例,打印一个实数,该数字是Johnny可以制作的最大容量,舍入到两个小数位.
示例输入:
2
20 14
20 16
Run Code Online (Sandbox Code Playgroud)
输出:
3.00
4.15
Run Code Online (Sandbox Code Playgroud)
输出细节
第一种情况:最大盒子的尺寸可以是3,1和1.
第二种情况:最大盒子的尺寸可以是7/3,4/3和4/3.
这是来自www.codechef.com的实践问题.这个名字是" 最好的盒子 ".我不想要这个代码.我想知道的是我们如何解决这个问题?任何帮助,将不胜感激.提前致谢.
我正在尝试执行一个程序,其中函数由指针指向.它如下: -
这是第一个使用"void"返回类型的程序.
#include<stdio.h>
#include<conio.h>
void CharPrint(char *ptr);
main()
{
char *str="Hello World";
void (*ptr1)(char *ptr);
ptr1=CharPrint;
if((*ptr1)(str))
printf("Done");
return 0;
}
void CharPrint(char *ptr)
{
printf("%s\n",ptr);
}
Run Code Online (Sandbox Code Playgroud)
它会引发许多错误.他们是:-

第二个方案如下: -
#include<stdio.h>
#include<conio.h>
int CharPrint(char *ptr);
main()
{
char *str="Hello World";
int (*ptr1)(char *ptr);
ptr1=CharPrint;
if((*ptr1)(str))
printf("Done");
return 0;
}
int CharPrint(char *ptr)
{
printf("%s\n",ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序运行没有任何打嗝.
输出是: -

我的问题是,在第一个输出中,为什么它在第9行显示"函数main中不允许的类型".其他行也引起了疑虑,但这条线最让我烦恼.有帮助吗?
我想解决的问题如下: -
给定一个整数长度为3的数组,返回一个元素"向左旋转"的数组,因此{1,2,3}产生{2,3,1}
我想出了以下代码: -
public int[] rotateLeft3(int[] nums) {
for(int i=0;i<2;i++)
swap(nums[i],nums[i+1]);
return nums;
}
public void swap(int a,int b)
{
int temp = a;
a = b;
b= temp;
}
Run Code Online (Sandbox Code Playgroud)
但是,它未成功运行.在C++的情况下,我可以将引用作为参数传递,问题将被排序,那么为什么不在这里发生?
以下代码正在运行: -
public int[] rotateLeft3(int[] nums) {
int temp = nums[0];
nums[0] = nums[1];
nums[1] = temp;
temp = nums[1];
nums[1] = nums[2];
nums[2] = temp;
return nums;
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码是完整的蛮力写作,我不是很喜欢它.你能否建议我如何让第一种方法起作用?
以下是codingbat的问题.
给定一个字符串,返回一个字符串,其中对于原始字符中的每个字符,有两个字符.例如:
Run Code Online (Sandbox Code Playgroud)doubleChar("The") ? "TThhee" doubleChar("AAbb") ? "AAAAbbbb" doubleChar("Hi-There") ? "HHii--TThheerree"
我有两个语句可以做到这一点,但注释中的语句没有给出例外输出:
public String doubleChar(String str) {
String str1 = "";
for(int i=0;i<str.length();i++)
{
//str1 += str.charAt(i) + str.charAt(i);
str1 += str.substring(i,i+1)+str.substring(i,i+1);
}
return str1;
}
Run Code Online (Sandbox Code Playgroud)
如果我将注释部分更改为str1 = str1 + str.charAt(i) + str.charAt(i),则输出是必需的.我无法理解这一点.如果连接没有,则它不适用于任何一种情况.你能帮帮我吗?
这是K&R给出的一个声明,printf()和putchar()可以交错.如果是,那么为什么以下代码没有提供所需的输出: -
#include"stdio.h"
void main()
{
char c,d;
printf("Enter the first character\n");
scanf("%c",&c);
printf("%c\n",c);
printf("Enter the second character\n");
d=getchar();
putchar(d);
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
每当我执行这个程序时,输出如下: -
Enter the first character a a Enter the second character
这是输出.如果我用putchar()替换printf()和用getchar()替换scanf(),也会发生这种情况.这为什么开心呢?
我有一个程序,旨在将10个整数从一个数组复制到另一个数组.它正在编译而没有任何错误.它如下: -
/* Program to copy a string from one array to another array and print the second array */
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int s[MAX]; // Original array
int c[MAX]; // Array which contains the copied contents of s[MAX]
int i;
printf("Enter the string of 10 characters");
for(i=0;i<MAX;i++) // Storing elements in the original array
{
scanf("%d",s[i]);
}
for(i=0;i<MAX;i++)
{
printf("%d",s[i]);
}
printf("\n");
for(i=0;i<MAX;i++) /*Copying the elements from the original array into the duplicate array*/ …Run Code Online (Sandbox Code Playgroud)